Working with Message Payloads
The ESB Profile of WSO2 Enterprise Integrator (WSO2 EI) provides support for JavaScript Object Notation (JSON) payloads in messages. The following sections describe how to work with JSON via the ESB Profile of WSO2 EI:
- 1 Handling JSON to XML conversion
- 2 Handling XML to JSON conversion
- 3 Converting a payload between XML and JSON
- 4 Accessing content from JSON payloads
- 5 Logging JSON payloads
- 6 Constructing and transforming JSON payloads
- 7 XML to JSON transformation parameters
- 8 Validating JSON messages
- 9 Troubleshooting, debugging, and logging
- 10 Converting plain text payloads to XML
Handling JSON to XML conversion
When building the XML tree, JSON builders attach the converted XML infoset to a special XML element that acts as the root element of the final XML tree. If the original JSON payload is of type object, the special element is <jsonObject/>. If it is an array, the special element is <jsonArray/>. Following are examples of JSON and XML representations of various objects and arrays.
Null objects
JSON:
{"object":null}XML:
<jsonObject>
<object></object>
</jsonObject>Empty objects
JSON:
{"object":{}}XML:
<jsonObject>
<object></object>
</jsonObject>Empty strings
JSON:
{"object":""}XML:
<jsonObject>
<object></object>
</jsonObject>Empty array
JSON:
[]XML (JsonStreamBuilder):
<jsonArray></jsonArray>XML (JsonBuilder):
<jsonArray>
<?xml-multiple jsonElement?>
</jsonArray>Named arrays
JSON:
{"array":[1,2]}XML (JsonStreamBuilder):
<jsonObject>
<array>1</array>
<array>2</array>
</jsonObject>XML (JsonBuilder):
<jsonObject>
<?xml-multiple array?>
<array>1</array>
<array>2</array>
</jsonObject>JSON:
{"array":[]}XML (JsonStreamBuilder):
<jsonObject></jsonObject>XML (JsonBuilder):
<jsonObject>
<?xml-multiple array?>
</jsonObject>Anonymous arrays
JSON:
[1,2]XML (JsonStreamBuilder):
<jsonArray>
<jsonElement>1</jsonElement>
<jsonElement>2</jsonElement>
</jsonArray>XML (JsonBuilder):
<jsonArray>
<?xml-multiple jsonElement?>
<jsonElement>1</jsonElement>
<jsonElement>2</jsonElement>
</jsonArray>JSON:
[1, []]XML (JsonStreamBuilder):
<jsonArray>
<jsonElement>1</jsonElement>
<jsonElement>
<jsonArray></jsonArray>
</jsonElement>
</jsonArray>XML (JsonBuilder):
<jsonArray>
<?xml-multiple jsonElement?>
<jsonElement>1</jsonElement>
<jsonElement>
<jsonArray>
<?xml-multiple jsonElement?>
</jsonArray>
</jsonElement>
</jsonArray>XML processing instructions (PIs)
Note the addition of xml-multiple processing instructions to the XML payloads whose JSON representations contain arrays. JsonBuilder (via StAXON) adds these instructions to the XML payload that it builds during the JSON to XML conversion so that during the XML to JSON conversion, JsonFormatter can reconstruct the arrays that are present in the original JSON payload. JsonFormatter interprets the elements immediately following a processing instruction to construct an array.
Special characters
When building XML elements, the EI handles the ‘$’ character and digits in a special manner when they appear as the first character of a JSON key. Following are examples of two such occurrences. Note the addition of the _JsonReader_PS_ and _JsonReader_PD_ prefixes in place of the ‘$’ and digit characters, respectively.
JSON:
{"$key":1234}XML:
<jsonObject>
<_JsonReader_PS_key>1234</_JsonReader_PS_key>
</jsonObject>JSON: