This documentation is for WSO2 Enterprise Service Bus version 4.9.0 . View documentation for the latest release.

All docs This doc

Versions Compared

Key

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

...

Table of Contents
maxLevel3

Anchor
MessageBuildersandFormatters
MessageBuildersandFormatters
JSON message builders and formatters

The ESB provides 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

Always use the same type of builder and formatter combination. Mixing different builders and formatters will cause errors at runtime.

Tip

If you want the ESB to handle JSON payloads that are sent using a media type other

...

than application/json, you must register the JSON builder and formatter for that media type in the following two files at minimum (for best results, register them in all Axis2 configuration files found in

...

the <ESB_HOME>/repository/conf/axis2

...

 directory):

  • <ESB_HOME>/repository/conf/axis2/axis2.xml
  • <ESB_HOME>/repository/conf/axis2/axis2_blocking_client.xml

For example, if the media type

...

is text/javascript, register the message builder and formatter as follows:

Code Block
languagehtml/xml
<messageBuilder contentType="text/javascript" 
               class="org.apache.synapse.commons.json.JsonStreamBuilder"/>

<messageFormatter contentType="text/javascript" 
                class="org.apache.synapse.commons.json.JsonStreamFormatter"/> 
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.

...

Code Block
languagehtml/xml
<property name="messageType" value="application/json" scope="axis2"/>
Infonote

JSON requests cannot be converted to xml if it contains invalid xml characters. 

Info

If you need to convert complex XML responses (e.g., XML with with xsi:type values), you will need to set the message type using the Property mediator as follows:

<property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"/>

You will also need to ensure you register the following message builder and formatter as specified in Message Builders and Formatters.

Code Block
<messageBuilder contentType="text/javascript" 
               class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>

<messageFormatter contentType="text/javascript" 
                class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/> 

Accessing content from JSON payloads

...