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:

...

Note that we have used the Property mediator to mark the outgoing payload to be formatted as JSON:

...

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.*">

...

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.

...

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:

...

Validating JSON messages

You can use the Validate mediator to validate JSON messages against a specified JSON schema as described in the rest of this section.

...

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.

...