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.

Image for post

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
Image for post

Step 3:

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

Image for post

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.

Image for post

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.

Image for post

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.

Image for post

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.

Image for post

#ibm #xml #api #api-development #json

How to convert between JSON and XML in IBM Integration Bus
14.30 GEEK