Find And Replace JSON Payload from the Application request to Target Request.

Not applicable

Hi,

I have JSON payload whose values should be changed once it reaches to the target endpoint.

Payload:

{ "RequestParam": { "UserName": "null", "Attributes": [ { "Values": [ { "fullName": Akbar sha, "Country": India, "Age": "24" } ], "Name": "Akbar", "city": "Banglore" } ], "EmpId": "null", "RollId": "12309876", "NewId":"987654" }, "contact":"akbar123@gmail.comj" }

I Need to find out UserName and EmpId values from payload if it is null then replace the values with the values saved in the cache.

Please suggest me some JS code to do this.

Solved Solved
1 1 1,570
1 ACCEPTED SOLUTION

Hi @sumiya

Couple of things -

1) The payload you pasted above is not a valid JSON. Assuming its a typo.

2) Since this is a JSON object, you can access the values directly using JavaScript policy

Assuming you have retrieved the values from Cache and set them to flow variables using Lookup Cache Policy already - then the JS policy will be

var payload = JSON.parse(request.content);


var empId = payload.RequestParam.EmpId;
var userName = payload.RequestParam.UserName;


if(empId===null || empId==="" || empId === "null"){
    payload.RequestParam.EmpId = context.getVariable("empId_cache"); 
}


if(userName===null || userName==="" || userName === "null"){
    payload.RequestParam.UserName = context.getVariable("userName_cache");
}


request.content = '';
request.content.asJSON = payload;

3) The other approach is to use the Extract Variable policy and then call a JavaScript policy to set the values from Cache (similar to above) only if those extracted variables are null or "null"

Hope this helps !

View solution in original post

1 REPLY 1

Hi @sumiya

Couple of things -

1) The payload you pasted above is not a valid JSON. Assuming its a typo.

2) Since this is a JSON object, you can access the values directly using JavaScript policy

Assuming you have retrieved the values from Cache and set them to flow variables using Lookup Cache Policy already - then the JS policy will be

var payload = JSON.parse(request.content);


var empId = payload.RequestParam.EmpId;
var userName = payload.RequestParam.UserName;


if(empId===null || empId==="" || empId === "null"){
    payload.RequestParam.EmpId = context.getVariable("empId_cache"); 
}


if(userName===null || userName==="" || userName === "null"){
    payload.RequestParam.UserName = context.getVariable("userName_cache");
}


request.content = '';
request.content.asJSON = payload;

3) The other approach is to use the Extract Variable policy and then call a JavaScript policy to set the values from Cache (similar to above) only if those extracted variables are null or "null"

Hope this helps !