Apache NiFi: JSON to SOAP nifi json to soap

Apache NiFi: JSON to SOAP

Apache NiFi is a powerful open source integration product. A challenge you might encounter when integrating systems is that one system can produce JSON messages and the other has a SOAP API available. In this blog post I’ll show how you can use NiFi to convert JSON input to a SOAP service call. This involves abstracting an AVRO schema for the JSON, converting it to XML and transforming the XML to a SOAP message.

Apache NiFi: JSON to SOAP nifi json to soap

In this example I’m using several publicly available websites. You should of course be careful with this. Do not copy/paste sensitive XML or JSON on these sites!

Input

I’ve used the following API to develop this example: https://api.covid19api.com/summary. It will give me a JSON like:

Apache NiFi: JSON to SOAP json input

Abstract an AVRO schema from a JSON sample

I’ve used the following site to generate an AVRO schema from the JSON sample: http://www.dataedu.ca/avro. This site has been created by Iraj Hedayati.

This created an AVRO schema like:

Apache NiFi: JSON to SOAP avro schema

If you’re going to fetch data from a Kafka topic which already uses an AVRO schema, you don’t have to create your own AVRO schema since then it is already provided. The schema is required by the NiFi ConvertRecord processor, XMLRecordSetWriter controller service, to be able to generate XML from JSON.

Generate an XSD schema from XML

The ConvertRecord processor in Apache NiFi generates an XML file like;

Apache NiFi: JSON to SOAP xml input

I used the following site (here) to generate an XSD for this XML. Why would I need to create an XSD? Having an XSD makes it easier to create an XSLT file. The resulting XSD looked like:

Apache NiFi: JSON to SOAP xsd input

Create a transformation from XML to SOAP

Next I manually created a target XSD based on a general SOAP definition. I took a sample from here. I generated an XSD for the SOAP message (similar as done before) and manually merged it with the previously generated schema definition. This target XSD looked like:

Apache NiFi: JSON to SOAP xsd output

In order to create a transformation from the source to the target, I’ve used Altova MapForce. There are of course other solutions available such as Oxygen XML Developer. When creating XSLTs, I recommend to use a tool which has a GUI and which is also a bit intelligent because it will save you work.

Apache NiFi: JSON to SOAP create

The resulting XSLT looked like:

Apache NiFi: JSON to SOAP xslt

NiFi

When the AVRO schema and XSLT file are ready, the NiFi work is relatively straightforward.

Apache NiFi: JSON to SOAP nifi flow

Some things to notice in the configuration:

For ConvertRecord you have to specify a Record Reader and a Record Writer

Apache NiFi: JSON to SOAP convertrecord

In the JsonTreeReader, a controller service, I have specified the previously generated AVRO schema;

Apache NiFi: JSON to SOAP jsontreereader

The XMLRecordSetWriter does not require any specific configuration.

For TransformXML I’ve used a SimpleKeyValueLookupService to store my XSLT file;

Apache NiFi: JSON to SOAP
Apache NiFi: JSON to SOAP simplekeyvaluelookupservice

Validating the result

In order to validate my request, in absence of an actual SOAP service I used the following in my docker-compose.yml file (see the base file here);

  echo:
    image: mendhak/http-https-echo:23
    environment:
      - HTTP_PORT=8888
      - HTTPS_PORT=9999
    ports:
        - "8888:8888"
        - "9999:9999"

This is an echo service which lets me view the request which is done in the docker logs. This way I can see the request and reply in NiFi and the request in the logging. The endpoint to call from NiFi in this case is http://echo:8888

Apache NiFi: JSON to SOAP soap message
Apache NiFi: JSON to SOAP reply

Of course this is not truly SOAP yet. You need to add a SOAPAction HTTP header (amongst other things). This is easy by adding a dynamic attribute on the InvokeHTTP processor.

Apache NiFi: JSON to SOAP soapaction

This header will then be set when calling the SOAP service. The below screenshot shows the reply from the echo service. This indicates the header has been set.

Apache NiFi: JSON to SOAP soapaction is send

One Response

  1. Tako de Jong April 25, 2022

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.