I need to replace multiple occurrence of a property value in json request using java-script. i have tried a java-script in JSFIDDLE and it worked but same code i used in APigee JavaScript but its not replacing the value. I have json data as follows: [{"Name":"app1","id":"1","groups":[{"id":"test1","name":"test grp45","desc":"this is a test group"},{"id":"test2","name":"test group 2","desc":"this is another test group"}]},{"Name":"app2","id":"2","groups":[{"id":"test3","name":"test group 4","desc":"this is a test group"},{"id":"test4","name":"test group 4","desc":"this is another test group"}]},{"Name":"app3","id":"3","groups":[{"id":"test5","name":"test group 5","desc":"this is a test group"},{"id":"test6","name":"test group 6","desc":"this is another test group"}]}] Here's what I have tried : var val = context.getVariabl("request.content"); context.setVariable("val", val); function findAndReplace(val1,value,replacevalue){ for(var x in val1){ if(typeof val1[x] == typeof {}){ findAndReplace(val1[x],value,replacevalue); } if(val1[x] == value){ val1["name"] = replacevalue; //break; // uncomment to stop after first replacement } } } findAndReplace(val,"test1","img"); var result= JSON.stringify(val); var obj = JSON.parse(result); context.setVariable("response.content", obj); I want to replace the value of "test1" to img Thanks, Ambili
Solved! Go to Solution.
Hi All,
Thanks, I found the answer .
i changed var val = context.getVariabl("request.content"); to var val=request.body.asJSON;
for getting content as JSON then it worked.
I assume context.getVariabl (method name error) is mistake while posting here not in code
Hi All,
Thanks, I found the answer .
i changed var val = context.getVariabl("request.content"); to var val=request.body.asJSON;
for getting content as JSON then it worked.
User | Count |
---|---|
7 | |
2 | |
2 | |
1 | |
1 |