How to modify existing value in Json Payload body passing request.

Not applicable

I have an API proxy and I passing json request in the body of request.

[{"status": "success", "ID":"200","UName":"xxxxx"}] and many more list of array..

I want to change the UName value. How to do it.

Solved Solved
1 3 9,109
1 ACCEPTED SOLUTION

HI @Madhav

Welcome to the community !!!

You can do that using JavaScript policy.

The code is something like:

var body = JSON.parse(request.content);
body.forEach(function(item){
   item.UName = "newValue"; //set value
});
request.content = ''; //To clear the request payload
request.content = JSON.stringify(body);

NOTE: Make sure this policy is within the Request flow of either Proxy or Target end point

Hope this helps !

View solution in original post

3 REPLIES 3

HI @Madhav

Welcome to the community !!!

You can do that using JavaScript policy.

The code is something like:

var body = JSON.parse(request.content);
body.forEach(function(item){
   item.UName = "newValue"; //set value
});
request.content = ''; //To clear the request payload
request.content = JSON.stringify(body);

NOTE: Make sure this policy is within the Request flow of either Proxy or Target end point

Hope this helps !

Hi @Madhav

You can try using a java script policy like this

var body= JSON.parse(context.getVariable("request.content"));
for(i=0;i<body.length;i++)
{
  body[i].UName="yyyy";

}
context.setVariable("request.content",JSON.stringify(body));

If you plan to perform JSON to JSON transformation I find JSMapr to be a good approach

https://github.com/mdunker/JSMapr

You can write custom functions to perform any value changes. This makes changes to JSON declarative and easier to maintain.

This is an overkill if you are just looking at one or two simple changes. I would go with the other options discussed here if that is the case.