JSONConverter
The JSONConverter component serves the need to convert data from either JSON format to XML format or from XML format to JSON format.
In recent times, JSON format is increasingly being used for serializing and transmitting structured data over a network connection like data transmission between server and a web application. Since JSON format is not widely adopted by all organizations yet, and as most of the data exchange is done through XML in earlier networks which have not deployed JSON, conversion from JSON to XML or vice-versa has become essential for data interoperability.
Configuration
Below is the JSONConverter Component Property Sheet (CPS) with Expert Properties (Click icon) enabled.
Figure 1: JSONConverter CPS with Expert Properties enabled
General configuration
Pre Processing XSL Configuration
Pre Processing XSL configuration can be used to transform request message before processing it. Click the small button on the property to configure the properties.
Refer to the Pre/Post Processing XSL Configuration section under Common Configurations topic for details regarding XSL configuration. The properties comprising this XSL configuration are:
- Provide XSL
- XSLT Engine
- Transformation source data
- Set transformation result as
Post Processing XSL Configuration
Post Processing XSL configuration can be used to transform response message before sending it to the output port.
As mentioned in the above section, refer to the Pre/Post Processing XSL Configuration section under the Common Configurations topic for details regarding XSL configuration.
Process Message Based on Property
The property helps components to skip certain messages from processing.
Refer the Process Message Based On a Property section under the Common Configurations topic.
Error Handling Configuration
Click the Ellipsis button to configure actions to be taken when an exception occurs; the attributes present in the dialog box are:
- JMS Error
- Response Generation Error
- Request Processing Error
- Invalid Request Error
For descriptions, please refer Error Handling section in Common Configurations page.
Convert JSON To XML ? (Yes/No)
- If Yes, it converts Input JSON to XML.
- If No, it converts Input XML to JSON.
Converting JSON To XML
If the value of the Convert JSON to XML property is set to 'yes', the component converts JSON to XML.
Figure 2: Component configuration - To convert JSON to XML
Show JSON types as attributes?
Whether or not to include JSON element types as attributes in Output XML.
Expand Array name to Child Elements ?
Enabling this will copy the parent JSONArray name to its child elements during JSON to XML conversion.
Output XSD Schema
Output port Schema can be provided here using schema editor which opens upon clicking the ellipsis button. It can be used to generate sample output XML for the component.
Figure 3: Output XSD Schema dialog box
Root Name
This field is used to set the name of root for the converted XML.
- To ignore the property, leave it blank.
- This is an Expert Property.
Element Name
This field is used to set the element name for each object of the array in the converted XML. When an array object in JSON format is converted into XML, each element of the JSON array is split into different XML fields, each with Element Name.
- To ignore the property, leave it blank.
- This is an Expert Property.
Converting XML to JSON
If the value of the Convert JSON to XML property is set to 'no', the component converts XML to JSON.
Figure 4: Component configuration - To convert XML to JSON
Skip Namespaces?
Whether or not to skip adding namespace declarations to XML elements while conversion.
Remove Namespace Prefix From Elements? (Yes/No)
Whether or not to remove the namespace prefix from XML elements when reading for XMLSerializer.
Indent Factor
String Indent value for JSON Object.
Input XSD Schema
Input port Schema can be provided here using the schema editor which opens upon clicking the ellipsis button. It can be used to generate sample input XML for the component.
Figure 5: Input XSD Schema dialog box
Formatting JSON
Whether to format the JSON text after conversion.
Skip Root Element?
Whether to skip Root Element while converting from XML to JSON. Determines whether the resulting JSON will start off with a top-most element whose name matches the XML root element.
Example
Consider the XML string below:
<a><x>1</x><y>2</y></a>
If the property is set to
"yes", the string is translated as:
CODE{"x": "1", "y": "2"}
"no", the string is translated as:
CODE{ "a": { "x": "1", "y": "2" }}
Enable Thread Pool
This property is used when there is a need to process messages in parallel within the component, still maintaining the sequence from the external perspective.
Set the value of the Enable Thread Pool property to "yes" to configure the Threadpool Configuration properties.
Figure 6: Threadpool Configuration
Pool Size
The number of requests to be processed in parallel within the component. Default value is '1'.
Batch Eviction Interval (in ms)
Time in milliseconds after which the threads are evicted in case of inactivity. New threads are created in place of evicted threads when new requests are received. Default value is '1000'.
Functional Demonstration
Scenario1: JSON to XML
This scenario shows how JSONConverter can be used to convert data in JSON Format to XML Format.
Create flow by connecting JSONConverter to Feeder and Display components. Send sample input (valid JSON) to check the response (transformed XML message). Use Configuration from Figure 2. The JSON input is sent from the Feeder and the corresponding XML output can be seen in Display.
Figure 7: Sample Event Process
Sample Input
Figure 8: Sample Input
Output
Figure 9: Sample Output
Scenario 2: XML to JSON
This scenario shows how JSONConverter can be used to convert data in XML Format to JSON Format.
Create flow by connecting JSONConverter to Feeder and Display components. Send sample input (valid XML) to check the response (transformed JSON message). Use Configuration from Figure 4.
The XML input is sent from the Feeder and the corresponding JSON output can be seen in Display.
Figure 10: Sample Event Process
Sample Input
Figure 11: Sample Input
Output
Figure 12: Sample Output
Scenario 3
Create flow by connecting JSONConverter to Feeder and Display components. Send sample input (valid JSON message) to check the response (transformed XML). Use Configuration from Figure 2.
The JSON input with array object is sent from the Feeder to the JSONConverter microservice and the corresponding XML output can be seen in Display. Here, the root of the XML is the same as given in Figure 2. In the output, the root element is set as "abc", which has been given in CPS configuration. Here in "Sample Input", "quantity" is an array object. While converting into XML, each element of the array is stored in "bcd" fields, which has been given in CPS configuration.
Figure 13: Sample Event Process
Sample Input
{"shiporder": { "@orderid": "orderid", "orderperson": "orderperson", "shipto": { "name": "name", "country": "country" }, "item": { "title": "title", "quantity": [12,15,23,45],} }} |
Output
<?xml version="1.0" encoding="UTF-8"?>
<abc>
<shiporder orderid="orderid">
<item>
<quantity>
<bcd>12</bcd>
<bcd>15</bcd>
<bcd>23</bcd>
<bcd>45</bcd>
</quantity>
<title>title</title>
</item>
<orderperson>orderperson</orderperson>
<shipto>
<country>country</country>
<name>name</name>
</shipto>
</shiporder>
</abc>
Useful Tips
- Output port and Input port XSD schemas can be used for mapping transformations for corresponding JSONtoXML and XMLtoJSON conversions.
In order to preserve the data type while converting an Input XML to JSON, specify the "type" attribute in the input XML.
Example
Input
CODE<root> <test type="integer">1</test> </root>
Output
CODE{ "root" : { "test" : 1 } }