JSON Enrich Mediator for WSO2 ESB


JSON support for WSO2 ESB [1] was introduced sometime back. But only small number of mediators support manipulating JSON payloads. In this article I am going to introduce a new mediator called JsonEnrichMediator [2], which work quite similar to existing Enrich mediator [3], but aiming JSON payloads. The specialty of this mediator is, since this is working with native JSON payload, JSON payload will not be converted to an XML representation. Hence there won’t be any data loss due to transformations.

Please note that this is a custom mediator I have created and will not ship with WSO2 ESB pack.

Configuring Mediator

  1. Clone the GitHub repository: https://github.com/Buddhima/JsonEnrichMediator
  2. Build the repository using maven (mvn clean install)
  3. Copy the built artifact in target folder to ESB_HOME/repository/components/dropins
  4. Download json-path-2.1.0.jar [5], json-smart-2.2.jar [6] and put them to the same folder (dropins).
  5. Start WSO2 ESB (sh bin/wso2server.sh)

Sample Scenario

For this article I am using a sample scenario which moves a JSON property within the payload. For that you need to add the following API to WSO2 ESB.

<api xmlns="http://ws.apache.org/ns/synapse" name="sampleApi" context="/sample">
<resource methods="POST" uri-template="/*">
<log level="full"/>
<source type="custom" clone="false" JSONPath="$.me.country"/>
<target type="custom" action="put" JSONPath="$" property="country"/>

The above configuration will take the value pointed by JSONPath “$.me.country” and move it to the main body. You can find further details about JSONPath at the location [4].

Once the API is deployed, you need to send following message to ESB.

curl -H "Content-Type: application/json"
-X POST -d '{
"country": "Sri Lanka",
"language" : "Sinhala"

The output of the ESB should look like below

"me": {
"language": "Sinhala"
"country": "Sri Lanka"


I have shown a simple use-case of using JSON Enrich Mediator. You can see the comprehensive documentation at the code repository [2].


[1] WSO2 ESB JSON support : https://docs.wso2.com/display/ESB500/JSON+Support

[2] Code Repository for JSON Enrich Mediator : https://github.com/Buddhima/JsonEnrichMediator

[3] WSO2 ESB Enrich Mediator : https://docs.wso2.com/display/ESB500/Enrich+Mediator

[4] JSON Path documentation : https://github.com/jayway/JsonPath/blob/json-path-2.1.0/README.md

[5] json-path-2.1.0 : https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path/2.1.0

[6] json-smart-2.2.1 : https://mvnrepository.com/artifact/net.minidev/json-smart/2.2.1


6 thoughts on “JSON Enrich Mediator for WSO2 ESB

  1. Hello,

    I am using Eclipse for development of APIs and Proxy services.

    Please suggest the folder location in eclipse where I can add JsonEnrichMediator.jar and respective jars to use them in Eclipse.


      1. Hi,
        Here you will find some alternatives. You may chose one of those according to your case.
        1. Payload-Factory: This is suitable in case where you need to completely replace the payload.
        2. Script mediator: You can tweak an existing JSON payload with this, in a similar syntax use in javascript
        3. Class mediator: If non of them work, you can create your own mediator and plug it as a class-mediator. You may be able to parameterize some values, so that you can reuse class-mediator at multiple places.

        Though Transaction Mediator is out of scope here, you may find useful samples on using it via following links;
        1. https://docs.wso2.com/display/ESB490/Transaction+Mediator
        2. https://docs.wso2.com/display/ESB490/Transaction+Mediator+Example
        3. https://docs.wso2.com/display/ESB481/Sample+657%3A+Distributed+Transaction+Management


  2. Hi,
    I know the context is different here but need some suggestions to evaluate WSO2 ESB.

    I have a request payload in JSON as below.
    { names: [ {“name”:”niraj”}, {“name=”atul”} ]}

    I need to iterate $body/names/name one by one and create a JSON Array as below.
    { resp: [ { “name”:”niraj”, “city”:”pune”} , {“name”:”atul”, “city”:”mumbai”} ] }

    Problem I am facing is that I am unable to create JsonArray inside foreach mediator in WSO2 ESB. I can’t see any option to define/use JsonArray in WSO2 ESB.

    Kindly suggest if you have any suggestions.


    1. Hi Abhishek,
      I believe following sample API will demonstrate your requirement.
      I have hard-coded the value “city” for the moment.

      <api xmlns="http://ws.apache.org/ns/synapse" name="testAPI1" context="/test">
         <resource methods="POST" uri-template="/*">
               <foreach expression="//names">
                     <payloadFactory media-type="xml">
                           <resp xmlns="">
                           <arg evaluator="xml" expression="//names/name/text()"/>
               <property name="messageType" value="application/json" scope="axis2"/>

      Sample curl request:

      curl -X POST \ \
        -H 'content-type: application/json' \
        -d '{ "names": [ {"name":"niraj"}, {"name":"atul"} ]}'


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s