Versions Compared

Key

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

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 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
languagexml
<messageBuilder contentType="text/javascript" 
               class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>

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

...

You can use JSON path expressions with following mediators:

MediatorUsage
Log

As a log property:

Code Block
languagehtml/xml
<log>
    <property name="location" 
              expression="json-eval($.coordinates.location[0].name)"/>
</log>
Property

As a standalone property:

Code Block
languagehtml/xml
<property name="location" 
              expression="json-eval($.coordinates.location[0].name)"/>
PayloadFactory  

As the payload arguments:

Code Block
languagehtml/xml
<payloadFactory media-type="json">
    <format>{"RESPONSE":"$1"}</format>
    <args>
        <arg evaluator="json" expression="$.coordinates.location[0].name"/>
    </args>
</payloadFactory>

IMPORTANT: You MUST omit the json-eval() method within the payload arguments to evaluate JSON paths within the PayloadFactory mediator. Instead, you MUST select the correct expression evaluator (xml or json) for a given argument.

Switch

As the switch source:

Code Block
languagehtml/xml
<switch source="json-eval($.coordinates.location[0].name)">
Filter

As the filter source:

Code Block
languagehtml/xml
<filter source="json-eval($.coordinates.location[0].name)" 
        regex="Eiffel.*">

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
Script
Script
Script mediator

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

synapse.commons.json.preserve.namespace

Preserves the namespace declarations in the JSON output in XML to JSON transformations.

false

synapse.commons.json.buildValidNCNames

Builds valid XML NCNames when building XML element names in XML to JSON transformations.

false

synapse.commons.json.output.autoPrimitive

Allows primitive types in the JSON output in XML to JSON transformations.

true

synapse.commons.json.output.namespaceSepChar

The namespace prefix separation character for the JSON output in XML to JSON transformations.

The default separation character is -

synapse.commons.json.output.enableNSDeclarations

Adds XML namespace declarations in the JSON output in XML to JSON transformations.

false

synapse.commons.json.output.disableAutoPrimitive.regex

Disables auto primitive conversion in XML to JSON transformations.

null

synapse.commons.json.output.jsonoutAutoArray

Sets the JSON output to an array element in XML to JSON transformations.

true

synapse.commons.json.output.jsonoutMultiplePI

Sets the JSON output to an xml multiple processing instruction in XML to JSON transformations.

true

synapse.commons.json.output.xmloutAutoArray

Sets the XML output to an array element in XML to JSON transformations.

true

synapse.commons.json.output.xmloutMultiplePI

Sets the XML output to an xml multiple processing instruction in XML to JSON transformations.

false
synapse.commons.enableXmlNilReadWriteHandles how empty XML elements with the 'nil' attribute are converted to JSON.false
synapse.commons.enableXmlNullForEmptyElement
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
languagejava
{
  "$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.

...