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:
...
Info | |||||
---|---|---|---|---|---|
If you need to convert complex XML responses (e.g., XML with with <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.
|
...
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:
|
Dynamic value support for JSONPath expressions
From EI 6.6.0 WUM timestamp 1596743391251 onwards, we are supporting dynamic JSONPath expressions.
Sample usage
json-eval($.[?(@.name=='{$ctx:customerName}')]) enclosing quotes needed when used with == operation
json-eval($.book.{$ctx:prep1}) no quotes needed when used as a part of the key
JSON path syntax
Suppose we have the following payload:
...
To log JSON payloads as JSON, use the Log mediator as shown below. The json-eval()
method returns the java.lang.String
representation of the existing JSON payload.
...
For more information on logging, see Troubleshooting, debugging, and logging below.
Constructing and transforming JSON payloads
...
PayloadFactory mediator
The PayloadFactory mediator provides the simplest way to work with JSON payloads. Suppose we have a service that returns the following response for a search query:
...
Anchor | ||||
---|---|---|---|---|
|
The Script mediator in JavaScript is useful when you need to create payloads that have recurring structures such as arrays of objects. The Script mediator defines the following important methods that can be used to manipulate payloads in many different ways:
...
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.commons.enableXmlNilReadWrite | Handles how empty XML elements with the 'nil' attribute are converted to JSON. | false |
| Handles how empty XML elements are converted to JSON. | true |
...
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" ] } |
In this example, the required schema for validating messages going through the Validate mediator is given as a registry key (i.e. schema\StockQuoteSchema.json
). You do not have any source attributes specified. Therefore, the schema will be used to validate the complete JSON body. The mediation logic to follow if the validation fails is defined within the on-fail element. In this example, the PayloadFactory mediator creates a fault to be sent back to the party, which sends the message.
...