Search code examples
javascriptwso2-api-managermediator

WSO2 Mediator that removes specific JSONobject occurrences within a JSON array


this is my first time transforming a response with json arrays in wso2. I am creating a wso2 outflow mediator that removes a specific json object within a json array response

Using payloadfactory sequence mediator, I was trying to remove the json object "price" from the json arrays. I followed the wso2 documentation but its ether i get an empty body response or the wso2 rejects the outflow sequence.

the sample response:

{
  "results": [
    {
      "name": "user",
      "item": "test",
      "price": {
        "pricePerItem": 2.0,
        "currency": "USD"
      },
      "stat": {
        "groupId": 3,
        "groupName": "DELIVERED",
      }
    },
    {
      "name": "user2",
      "item": "test2",
      "price": {
        "pricePerItem": 4.0,
        "currency": "USD"
      },
      "stat": {
        "groupId": 4,
        "groupName": "DELIVERED",
      }
    }
    ]
}

my outflow code:

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="test_outflow.xml" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
  <payloadFactory media-type="json">
        <format>
                $1
        </format>
        <args>
            <arg evaluator="json" expression="$.results"/>
        </args>
</payloadFactory>
 <script language="js"><![CDATA[
    var payload = mc.getPayloadJSON();
    results = payload.results;
    response = new Array();
           for (i = 0; i &lt; results.length; ++i) {
           delete payload[i].id;
           response[i] = l;
           }
           mc.setPayloadJSON(response);
           ]]></script>
 <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
</sequence>

expected results:

{
  "results": [
    {
      "name": "user",
      "item": "test",
      "stat": {
        "groupId": 3,
        "groupName": "DELIVERED",
      }
    },
    {
      "name": "user2",
      "item": "test2",
      "stat": {
        "groupId": 4,
        "groupName": "DELIVERED",
      }
    }
    ]
}

If any of you have experience with this kind of situation? thanks!


Solution

  • You can use the following inline script to achieve your requirement

    <script language="js" xmlns="http://ws.apache.org/ns/synapse"><![CDATA[
        var payload = mc.getPayloadJSON();
        var results = payload.results;
        var response = new Array();
        for (var i = 0; i < results.length; ++i) {
            delete results[i].price;
            response[i] = results[i];
        }
        payload.results = response;
        mc.setPayloadJSON(payload);
    ]]></script>
    

    Given below is a sample out-sequence (synapse) developed for the following mock requirement

    Actual Response (before mod):

    {
      "results": [
        {
          "name": "Athiththan",
          "price": {
            "value": 120
          }
        },
        {
          "name": "athiththan11",
          "price": {
            "value": 100
          }
        }
      ]
    }
    

    Excepted Response (after mod):

    {
      "results": [
        {
          "name": "Athiththan"
        },
        {
          "name": "athiththan11"
        }
      ]
    }
    

    Out Sequence Synapse configuration:

    <?xml version="1.0" encoding="UTF-8"?>
    <outSequence xmlns="http://ws.apache.org/ns/synapse">
        <script language="js"><![CDATA[
            var payload = mc.getPayloadJSON();
            var results = payload.results;
            var response = new Array();
            for (var i = 0; i < results.length; ++i) {
                delete results[i].price;
                response[i] = results[i];
            }
            payload.results = response;
            mc.setPayloadJSON(payload);
        ]]></script>
        <send/>
    </outSequence>