In one of my previous tutorials, I explained step by step how to develop JSON based REST Service in TIBCO BW. For REST based services, we often come across use cases where we need to Convert XML to JSON and JSON To XML REST. For this purpose, TIBCO REST & JSON palette provided two activities Parse JSON and Render JSON which serve the purpose. In this post, I will explain with an example how we can use Parse JSON and Render JSON activities to convert a JSON String into XML and vice versa.
Example Scenario for JSON to XML and XML to JSON Conversion
To get a fair idea of using Parse JSON and Render JSON activities in TIBCO BW, consider an example scenario where we have following JSON data as an input:
{ “EMPLOYEE”:{
“EMP_FIRST_NAME”:”Ajmal”,
“EMP_LAST_NAME”: “Abbasi”,
“EMP_AGE”:”30″ }
}
And our requirement is to parse this JSON into an XML Format and then construct another JSON string with first name and last name combined together as EMP_FULL_NAME:
{ “EMPLOYEE”:{
“FULL_NAME”:”Ajmal Abbasi”,
“AGE”:”30″ }
}
For the above requirement, let’s create a simple process and add a string parameter to the Start activity by defining it in Output Editor Tab. We will input the JSON string to the process when testing through this element.
Next, add Parse JSON activity in the process and map JSON String from the start activity as an input to this activity as shown below:
Now in the Output Editor tab, we create a complex element corresponding to our JSON structure. Please note that you can reference to a Schema if you have an XML Schema for this. For the simplicity of this tutorial, I haven’t created the schema and will go with defining complex element instead as shown below:
These configurations of Parse JSON activity are enough to convert our input JSON String into the XML structure according to the structure specified in output editor tab.
Now, to convert the XML back into the JSON format of our required structure with first name and last name combined, Let’s add Render JSON activity to the process.
In the Input Editor tab of Render JSON activity, define complex element corresponding to the required JSON structure as shown below:
In the input tab of Render JSON activity, we will do the required mappings. Here we are concatenating the names obtained from the output of Parse XML by adding an additional space between first and last name.
That’s it for Parse JSON and Render JSON activities configurations. Now let us load this process in designer tester and run it by giving our first JSON as input.
As you can see in below screenshot, we have received output JSON in the format as required:
Feel free to comment below if you need any further clarification.