The JSON2XML policy enables developers to convert messages from the JavaScript object notation (JSON) format to the extensible markup language (XML) format. This policy is useful for enabling backend XML services to support RESTful apps that require JSON, for example, due to a lack of an XML parsing capabilities on the client.
The attributes available are as shown in the figure and described in the following table.
Configuration
The properties that have to be configured to use the policy are described below.
![](/documentation/download/attachments/10223842/image2014-10-7%2020%3A36%3A4.png?version=2&modificationDate=1412765061000&api=v2)
Figure 1: JSON to XML Policy Configuration attributes
Property | Description |
---|
JSON types as attributes | Whether JSON element types should be included 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. |
Root Name | This field is used to set the name of root for the converted XML. If it is set to null, then this property is ignored. |
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. If it is set to null, then this property is ignored. |
Example
Code Block |
---|
|
{"shiporder": { "@orderid": "orderid", "orderperson": "orderperson", "shipto": { "name": "name", "country": "country" },
"item": { "title": "title", "quantity": [12,15]} }} |
Output
Various outputs depending on changes in the configuration are given below.
Code Block |
---|
title | Root Name property set as 'Root' and Element Name propety as 'element' |
---|
language | xml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<shiporder orderid="orderid">
<item>
<quantity>
<element>12</element>
<element>15</element>
</quantity>
<title>title</title>
</item>
<orderperson>orderperson</orderperson>
<shipto>
<country>country</country>
<name>name</name>
</shipto>
</shiporder>
</Root> |
Code Block |
---|
title | JSON Types as Atrributes property and Expand Array Name to Child Elements property enabled |
---|
language | xml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<shiporder class="object" orderid="orderid">
<item class="object">
<quantity type="number">12</quantity>
<quantity type="number">15</quantity>
<title type="string">title</title>
</item>
<orderperson type="string">orderperson</orderperson>
<shipto class="object">
<country type="string">country</country>
<name type="string">name</name>
</shipto>
</shiporder> |
Code Block |
---|
title | JSON Types as Atrributes property and Expand Array Name to Child Elements property disabled |
---|
language | xml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="orderid">
<item>
<quantity>1215</quantity>
<title>title</title>
</item>
<orderperson>orderperson</orderperson>
<shipto>
<country>country</country>
<name>name</name>
</shipto>
</shiporder> |
Code Block |
---|
title | JSON Types as Atrributes property disabled and Expand Array Name to Child Elements property enabled |
---|
language | xml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="orderid">
<item>
<quantity>12</quantity>
<quantity>15</quantity>
<title>title</title>
</item>
<orderperson>orderperson</orderperson>
<shipto>
<country>country</country>
<name>name</name>
</shipto>
</shiporder> |