How to convert between JSON and XML in IBM Integration Bus
By: Ahmed Ahsan Khan
Tags: Api Development, IBM, Xml, Json, API

Photo by Shahadat Rahman on Unsplash

Converting between message formats is one of the main features of an integration software. The need for this is because applications talk in different formats. some applications are exposed as SOAP API which is based on XML and others are exposed as REST API which is based on JSON format. So In this tutorial I will be explaining how conversion between JSON and XML is done in IIB using ESQl (also known as Extended SQL language) which is native language to IIB.

Requirements:

In this tutorial we will be using postman to send request to our application, IBM App Connect to create REST API for conversion between XML and JSON depending on the input we receive.

Step 1:

In your App Connect Toolkit, start by creating a REST API.

Step 2:

Open the REST descriptor, add new resource by clicking on the (+) next to the resources. Name it /converter and select post operation. Click Apply then OK

Step 3:

Create a sub flow by clicking on the “Create a sub flow” icon as shown.

Step 4:

Once we have our sub flow ready, we can start with our development. Add the following nodes and connect it as shown. Double click on the node so that it will generate the ESQL file to avoid any errors.

Message flow of our REST API

REST API in IIB by default parse data in JSON format

The reason for designing such a message flow is that REST API in IIB by default parse data in JSON format. In our tutorial we need to convert between XML and JSON and vise versa. So we will need to configure our flow to accept XML formats as well. For that we have used header param from our request header and routed our flow based on its value.

Properties for route node

In case of match output, it will route to “Reset Content Descriptor” node and it will make the message domain as XMLNSC for parsing. In case of JSON output the flow will go to the compute node for conversion. At any point in the flow, if the parsing fails (either JSON or XML) it will send an error from the error compute node.

Property of Reset Content Descriptor Node

Step 5: Actual Code

Till this step, we have our configurations all setup and our message flow ready. Now it is time to actually see the coding part. The image attached below shows how the ESQL code is working to differentiate between XML and JSON format and finally doing the actual conversion.

The complete project is uploaded on my github repository at the link provided below:

Step 6: Testing with postman

For all those who have not used postman yet, Postman is a software that is used to test REST APIs. Now that our REST API is ready, we have deployed it in our integration server and will test the API using postman.

Test JSON to XML

In the postman go to header tab and create Content-Type as “application/json”.

Then open the body tag and enter any JSON object which you want to convert to XML and the application will return you the XML with the root element as “Root” as shown below.

2. Test XML to JSON

In the postman go to header tab and change Content-Type to“application/xml”.

Then open the body tag and enter any valid XML text which you want to convert to JSON and the application will return you its respective JSON as shown.

Conclusion:

In this article we have moved a step further than theoretically explaining about JSON and XML formats. We have created a REST API using App Connect Toolkit and ESQL for converting between the two message formats which are widely used in the industry.

Read More:

Happy Learning.