WSO2 EI provides support for JavaScript Object Notation (JSON) payloads in messages. The following sections describe how to work with JSON in the EI:
...
Other message builders and formatters can be enabled by adding the passthruJsonBuilder
and passthruJsonFormatter
parameters the required parameters to the axis2.xml
file as explained below.
If you want to convert the JSON representation to XML before the mediation flow begins, you need to add the message builder and formatter shown below. Note that some data loss can occur during the JSON to XML to JSON conversion process.
<parameter name="passthruJsonBuilder">org.apache.synapse.commons.json.JsonBuilder</parameter>
<parameter name="passthruJsonFormatter">org.apache.synapse.commons.json.JsonFormatter</parameter>
The following builders and formatters can be enabled for JSON mapping in data services.
- <parameter name="dsJsonBuilder">
org.apache.axis2.json.gson.JsonBuilder
</parameter> - <parameter name="dsJsonFormatter">
org.apache.axis2.json.JSONMessageFormatter
</parameter>
The following builders and formatters can also enabled. Note that this is necessary for compatibility with previous versions of the EI (WSO2 ESB):
...
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
...
Info | ||
---|---|---|
Set the value of the
|
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.
...
You can use JSON path expressions with following mediators:
Mediator | Usage | |||||
---|---|---|---|---|---|---|
Log | As a log property:
| |||||
Property | As a standalone property:
| |||||
PayloadFactory | As the payload arguments:
IMPORTANT: You MUST omit the | |||||
Switch | As the switch source:
| |||||
Filter | As the filter source:
|
JSON path syntax
Suppose we have the following payload:
...
The following table summarizes sample JSONPath expressions and their outputs:
Expression | Result |
---|---|
$. | { "id":12345, "id_str":"12345", "array":[1, 2, [[],[{"inner_id":6789}]]], "name":null, "object":{}, "$schema_location":"unknown", "12X12":"image12x12.png"} |
$.id | 12345 |
$.name | null |
$.object | {} |
$.['$schema_location'] | unknown |
$.12X12 | image12x12.png |
$.array | [1, 2, [[],[{"inner_id":6789}]]] |
$.array[2][1][0].inner_id | 6789 |
You can learn more about JSONPath syntax here.
...
XML to JSON transformation parameters
You can use XML to JSON transformation parameters add the parameters listed below to the synapse.properties
file (stored in the <EI_HOME>/conf/
directory) when you need to transform XML formatted data into the JSON format.Following are the XML to JSON transformation parameters and their descriptions:
Parameter | Description | Default Value |
---|---|---|
| Preserves the namespace declarations in the JSON output in XML to JSON transformations. | false |
| Builds valid XML NCNames when building XML element names in XML to JSON transformations. | false |
| Allows primitive types in the JSON output in XML to JSON transformations. | true |
| The namespace prefix separation character for the JSON output in XML to JSON transformations. | The default separation character is - |
| Adds XML namespace declarations in the JSON output in XML to JSON transformations. | false |
| Disables auto primitive conversion in XML to JSON transformations. | null |
| Sets the JSON output to an array element in XML to JSON transformations. | true |
| Sets the JSON output to an xml multiple processing instruction in XML to JSON transformations. | true |
| Sets the XML output to an array element in XML to JSON transformations. | true |
| Sets the XML output to an xml multiple processing instruction in XML to JSON transformations. | false |
synapse.json.to. |
xml. |
processing. |
instruction. |
Sets an empty element to an empty JSON string in XML to JSON transformations.
true
enabled | Enables adding processing instructions when performing JSON to XML conversions. For example, you can add processing instructions to identify array elements in JSON payloads with array indices (i.e., marked with "[ ]") and process them to XML.
|
Anchor | ||||
---|---|---|---|---|
|
...
The parameters available in this section are as follows.
Parameter Name | Description |
---|---|
Schema keys defined for Validate Mediator | This section is used to specify the key to access the main schema based on which validation is carried out, as well as to specify the JSON, which needs to be validated. |
Source | The JSONPath expression to extract the JSON that needs to be validated. E.g: json-eval($.msg)" |
Following example use the below sample schema StockQuoteSchema.json
file. Add this sample schema file (i.e. StockQuoteSchema.json
) to the following Registry path: conf:/schema/StockQuoteSchema.
json. For instructions on adding the schema file to the Registry path, see Adding a Resource.
Tip |
---|
When adding this sample schema file to the Registry, specify the Media Type as application/json. |
Code Block | ||
---|---|---|
| ||
{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "getQuote": { "type": "object", "properties": { "request": { "type": "object", "properties": { "symbol": { "type": "string" } }, "required": [ "symbol" ] } }, "required": [ "request" ] } }, "required": [ "getQuote" ] } |
...