Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

JSON message builders and formatters

There are two types of message builders and formatters for JSON. The default builder and formatter keep the JSON representation intact without converting it to XML. You can access the payload content using JSON Path or XPath and convert the payload to XML at any point in the mediation flow.

...

Note

When you modify the builders/formatters in Axis2 configuration, make sure that you have enabled only one correct message builder/formatter pair for a given media type.

XML representation of JSON payloads

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:

Code Block
languagejavascript
{"object":null}

...

Code Block
languagehtml/xml
<jsonObject>
    <object></object>
</jsonObject>
Empty objects

JSON:

Code Block
languagejavascript
{"object":{}}

...

Code Block
languagehtml/xml
<jsonObject>
   <object></object>
</jsonObject>
Empty strings

JSON:

Code Block
languagejavascript
{"object":""}

...

Code Block
languagehtml/xml
<jsonObject>
   <object></object>
</jsonObject>
Empty array

JSON:

Code Block
languagejavascript
[]

...

Code Block
languagehtml/xml
<jsonArray>
   <?xml-multiple jsonElement?>
</jsonArray>
Named arrays

JSON:

Code Block
languagejavascript
{"array":[1,2]}

...

Code Block
languagehtml/xml
<jsonObject>
   <?xml-multiple array?>
</jsonObject>
Anonymous arrays

JSON:

Code Block
languagejavascript
[1,2]

...

Code Block
languagehtml/xml
<jsonArray>
   <?xml-multiple jsonElement?>
   <jsonElement>1</jsonElement>
   <jsonElement>
       <jsonArray>
           <?xml-multiple jsonElement?>
       </jsonArray>
   </jsonElement>
</jsonArray>
XML processing instructions (PIs)

Note that 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 ‘$’ character and digits are handled 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.

...

Code Block
languagehtml/xml
<jsonObject>
   <_JsonReader_PD_32X32>image_32x32.png</_JsonReader_PD_32X32>
</jsonObject>

Converting a payload between XML and JSON

To convert an XML payload to JSON, set the messageType property to application/json in the axis2 scope before sending message to an endpoint. Similarly, to convert a JSON payload to XML, set the messageType property to application/xml or text/xml. For example:

...