user2326831
user2326831

Reputation: 161

How to concatenate two JSON responses in wso2 esb

The two jsons i need to concatenate to convert it into a single valid json are :

   {

    "first": true,
    "second": {
        "name": "manoj",
        "age": "45"
    },
    "third": {
        "fourth": [{

                "class": "test12",
                "salary": "123456"
            },

            {
                "class": "test23",
                "salary": "15678"
            }
        ],
        "fifth": "hello"
    }
   }

and

 [{
        "item1": "123456",
        "item2": "5678"

    },
  {
        "item1": "8976",
        "item2": "abcd"

    }]

Is it possible to concatenate these two without using any jquery. I need something related to wso2 esb code. I tried using enrich and other mediators but no luck so far.

Upvotes: 2

Views: 1648

Answers (1)

You can concat the jsons using WSO2 ESB Payload Factory mediator as follows,

<api xmlns="http://ws.apache.org/ns/synapse" name="ConcatAPI" context="/concat">
<resource methods="GET">
  <inSequence>
     <call>
        <endpoint>
           <http method="GET" uri-template="http://www.mocky.io/v2/56b2d88c13000057518945d4"/>
        </endpoint>
     </call>
     <enrich>
        <source type="body" clone="true"/>
        <target type="property" property="first-json"/>
     </enrich>
     <log level="custom">
        <property name="First json" expression="get-property('first-json')"/>
     </log>
     <call>
        <endpoint>
           <http method="GET" uri-template="http://www.mocky.io/v2/56b2d87d1300007c518945d3"/>
        </endpoint>
     </call>
     <payloadFactory media-type="xml">
        <format>
           <completeJson xmlns="">
              <firstjson>$1</firstjson>
              <secondjson>$2</secondjson>
           </completeJson>
        </format>
        <args>
           <arg evaluator="xml" expression="get-property('first-json')"/>
           <arg evaluator="xml" expression="$body"/>
        </args>
     </payloadFactory>
     <property name="messageType" value="application/json" scope="axis2"/>
     <send/>
  </inSequence>
  <outSequence/>
  <faultSequence/>
</resource>
</api>

Note that i have retrieved your jsons from mocked services from mocky.io web site.

Thanks.

Upvotes: 4

Related Questions