Skip to content Skip to sidebar Skip to footer

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 Usin

Solution 1:

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>

Post a Comment for "WSO2 Mediator That Removes Specific JSONobject Occurrences Within A JSON Array"