When a request comes to the API Manager, it sends the response in the same format of the request. For example, the API Manager handles JSON to JSON transformations out of the box. If the backend does not accept messages of the same content type of the request message, it must be transformed to a different format. The API Gateway of the API Manager handles these transformations using message builders and formatters.
When a message comes in to the API Gateway, the receiving transport selects a message builder based on the message's content type. It uses that builder to process the message's raw payload data and convert it into JSON. Conversely, when sending a message out from the Gateway, a message formatter is used to build the outgoing stream from the message. As with message builders, the message formatter is selected based on the message's content type.
Note that if you edit an API's synapse configuration as mentioned in this guide and then go back to the API Publisher and save the API, your changes will be overwritten. Therefore, we do not recommend changing the API's synapse configuration directly. The recommended way to extend an API's mediation flow is by engaging
Also see the following sections in the WSO2 EI documentation. WSO2 EI is used to implement the API Gateway through which API messages are transformed:
- Accessing content from JSON payloads
- Logging JSON payloads
- Constructing and transforming JSON payloads
- Troubleshooting, debugging, and logging
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 the JSON Path or XPath and convert the payload to XML at any point in the mediation flow.
If you want to convert the JSON representation to XML before the mediation flow begins, use the following builder and formatter instead. Note that some data loss can occur during the JSON -> XML -> JSON conversion process.
The builders and formatters are configured respectively in the
messageFormatters sections of the Axis2 configuration files located in the
<PRODUCT_HOME>/repository/conf/axis2 directory. Both types of JSON builders use StAXON as the underlying JSON processor.
The following builders and formatters are also included for compatibility with older API Manager versions:
Always use the same type of builder and formatter combination. Mixing different builders and formatters will cause errors at runtime.
If you want 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
For example, if the media type is
To support having spaces inside JSON attributes, change the default JSON builder and formatter to the following pair in either <APIM_HOME>/repository/conf/axis2/axis2.xml (super tenant scenario) or <APIM_HOME>/repository/conf/axis2/tenant-axis2.xml (tenant scenario) file:
To support use cases for JSON payloads with arrays, change the default JSON builder and formatter to the following pair in either
<APIM_HOME>/repository/conf/axis2/axis2.xml (super tenant scenario) or
<APIM_HOME>/repository/conf/axis2/tenant-axis2.xml (tenant scenario) file:
Else, in JSON to XML conversion, there might be issues as below:
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.
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.
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_PD_ prefixes in place of the ‘$’ and digit characters, respectively.
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
text/xml. For example:
An example command to invoke above API:
If the request payload is as follows:
The response payload will look like this:
Note that we have used the Property Mediator page on WSO2 EI documentation.to mark the outgoing payload to be formatted as JSON. For more information about the Property Mediator, see the
Similarly if the response message needs to be transformed, set the messageType property in the outSequence.
XML to JSON Transformation Parameters
See JSON Transformation Parameters for additional parameters for converting XML to JSON.