How can i iterate json response array and update/replace values on selected property

Not applicable

Here is my final json response:

{ "processResponse":{ "high":[ { "productName":"Samsung S3", "IMAGELOCATION":" \nhttp://198.46.51.87/369-871-original%20Promotion%20Banner042016B.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-ghdk" }, { "productName":"SamsungCase Cover", "IMAGELOCATION":" \nhttp://198.46.51.87/369-871-original%20Promotion%20Banner042016B.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-dgtt" }, { "productName":"4GOfferGold", "IMAGELOCATION":" \nhttp://198.46.51.87/369-871-original%20Promotion%20Banner042016B.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-tyup" } ] } }

in this i need to change IMAGELOCATION values in 2nd and 3rd products with other imageurl. This response will be dynamic based of some other parameter. I tried with "findAndReplace" function but its changing one value only at all places. Please suggest javascript to do this.

Solved Solved
0 10 29.1K
1 ACCEPTED SOLUTION

Hi @u.ganeshrao.gangoji,

I have added a screenshot of the javascipt code apigee.jpg that you can use to achieve this. Please refer the same but you might need to modify few conditions as per your requirement. Let me know if this helps.

View solution in original post

10 REPLIES 10

Hi @u.ganeshrao.gangoji

Please see below code to traverse each element of the array. You should have a condition to identify which array element should be replaced and add that code in the innermost loop.

var input_data = JSON.parse(context.getVariable("request.content"));

var arrayField = input_data.processResponse.high;

for (var i=0;i<arrayField.length;i++){

var currentElement=arrayField[i];

for (var key in currentElement){

if (currentElement.hasOwnProperty(key)){

print("key="+key+" value="+currentElement[key]);

//enter you condition to identify if this is the array element that needs replacement and of its imagelocation field and add code here. Something like make a flag=true once array element is matched and once you replace value make flag=false so that it will become true for next element match.

}

}

}

Hi @u.ganeshrao.gangoji

To update/replace values of selected json subproperty , you can try below code in the innermost loop.

referring the code by @santosh_ghalsasi;

input_data.processResponse.high[i].IMAGELOCATION="abc";

this will update the value of field IMAGELOCATION in each block to "abc".

And for the final Json output, you can add:

context.setVariable("response.content",JSON.stringify(input_data));

thannks. I tried with following:

var input_data = JSON.parse(context.getVariable("response.content")); var arrayField = input_data.processResponse.high; var flag; for (var i=0;i<arrayField.length;i++){ var currentElement=arrayField[i]; for (var key in currentElement){ if (currentElement.hasOwnProperty(key)){ if(response_json.processResponse.high[1].IMAGELOCATION == currentElement[key]){ currentElement[key].push("image"); context.setVariable("flag", "false"); }else{ context.setVariable("flag", "true"); } print("key="+key+" value="+currentElement[key]); } } }

its not working, getting error on push method

Actually i need to replace value of IMAGELOCATION element in each product with different values.

Can you share the expected output.

Not applicable

This is my requirement. Where i have changed imaglocation field value with "image1" and "image2" urls.

{ "processResponse":{ "high":[ { "productName":"Samsung S3", "IMAGELOCATION":" \nhttp://198.46.51.87/369-871-original%20Promotion%20Banner042016B.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-GCIL0" }, { "productName":"SamsungCase Cover", "IMAGELOCATION":" \nhttp://test/image1.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-GCILK" }, { "productName":"4GOfferGold", "IMAGELOCATION":" \nhttp://test/image2.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-GA9GI" } ] } }

Not applicable

This is my actual requirement. Where i have added "image1" and "image2" urls in 2nd & 3rd products.

{ "processResponse":{ "high":[ { "productName":"Samsung S3", "IMAGELOCATION":" \nhttp://198.46.51.87/369-871-original%20Promotion%20Banner042016B.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-GCIL0" }, { "productName":"SamsungCase Cover", "IMAGELOCATION":" \nhttp://test/image1.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-GCILK" }, { "productName":"4GOfferGold", "IMAGELOCATION":" \nhttp://test/image2.jpg", "DESCRIPTION":"Basic backup of essential mobile data e.g contacts,messages,appdata upto 8GB", "OFFERID":"1-GA9GI" } ] } }

Hi @u.ganeshrao.gangoji,

I have added a screenshot of the javascipt code apigee.jpg that you can use to achieve this. Please refer the same but you might need to modify few conditions as per your requirement. Let me know if this helps.

Hi Gargi, Thank you so much. This code has resolved my requirement.Thanks again 🙂

Not applicable

{
"environments": [
{
"dimensions": [
{
"metrics": [
{
"name": "sum(message_count)",
"values": [
{
"timestamp": 1492473600000,
"value": "1.0"
},
{
"timestamp": 1492387200000,
"value": "878.0"
},
{
"timestamp": 1492300800000,
"value": "115.0"
},
{
"timestamp": 1492214400000,
"value": "1161.0"
},
{
"timestamp": 1492128000000,
"value": "305.0"
},
{
"timestamp": 1492041600000,
"value": "540.0"
},
{
"timestamp": 1491955200000,
"value": "134.0"
}
]
},
{
"name": "sum(error_count)",
"values": [
{
"timestamp": 1492473600000,
"value": "0.0"
},
{
"timestamp": 1492387200000,
"value": "366.0"
},
{
"timestamp": 1492300800000,
"value": "0.0"
},
{
"timestamp": 1492214400000,
"value": "36.0"
},

If I have above response, how do I add all values for message_count and also for error_count and then show via assign message policy ?