Thursday, December 1, 2016

How to use WSO2 ESB Enrich Mediator To Remove Elements from Payload.

The Enrich Mediator can process a message based on a given source configuration and then perform the specified action on the message by using the target configuration. In this example it is used to remove some elements from the message payload. In this example we need to find jsonObject elements which has jsonArray elements within that and remove the jsonObject element for them.

Sample Request:


<root:rootelement xmlns:root="www.test.com">
   <jsonObject xmlns="http://ws.apache.org/ns/synapse">
      <jsonArray>
         <jsonElement>
            <account_name>XYZ</account_name>
            <account_id>20</account_id>
         </jsonElement>
      </jsonArray>
   </jsonObject>
   <jsonObject>
      <account_name>DEF</account_name>
      <account_id>22</account_id>
   </jsonObject>
   <jsonObject xmlns="http://ws.apache.org/ns/synapse">
      <jsonArray>
         <jsonElement>
            <account_name>PQR</account_name>
            <account_id>10</account_id>
         </jsonElement>
         <jsonElement>
            <account_name>JKL</account_name>
            <account_id>11</account_id>
         </jsonElement>
         <jsonElement>
            <account_name>QWE</account_name>
            <account_id>12</account_id>
         </jsonElement>
      </jsonArray>
   </jsonObject>
   <jsonObject>
      <account_name>ABC</account_name>
      <account_id>42</account_id>
   </jsonObject>
</root:rootelement>

Sample Response:


<root:rootelement xmlns:root="www.test.com">
   <jsonArray xmlns="http://ws.apache.org/ns/synapse">
      <jsonElement>
         <account_name>XYZ</account_name>
         <account_id>20</account_id>
      </jsonElement>
   </jsonArray>
   <jsonObject>
      <account_name>DEF</account_name>
      <account_id>22</account_id>
   </jsonObject>
   <jsonArray xmlns="http://ws.apache.org/ns/synapse">
      <jsonElement>
         <account_name>PQR</account_name>
         <account_id>10</account_id>
      </jsonElement>
      <jsonElement>
         <account_name>JKL</account_name>
         <account_id>11</account_id>
      </jsonElement>
      <jsonElement>
         <account_name>QWE</account_name>
         <account_id>12</account_id>
      </jsonElement>
   </jsonArray>
   <jsonObject>
      <account_name>ABC</account_name>
      <account_id>42</account_id>
   </jsonObject>
</root:rootelement>


Example Proxy Service:

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestXPath"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <foreach expression="//*[local-name()='jsonObject']">
            <sequence>
               <filter xpath="boolean(//*[local-name()='jsonObject']/*[name()='jsonArray'])">
                  <then>
                     <enrich>
                        <source clone="true" xpath="//*[local-name()='jsonArray']"/>
                        <target type="body"/>
                     </enrich>
                  </then>
               </filter>
            </sequence>
         </foreach>
         <respond/>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>

No comments:

Post a Comment