I want to put if else condition in json payload

I have json payload in response .

{ 
 "speed":"25.88",
 "Foo":"Bar",
 "Latitude":"Something"
}

Except: If speed >50 i want to print "speed:""high" or "low". The rest of the data should remain as it is. Kindly someone help me.

Solved Solved
1 1 25.5K
1 ACCEPTED SOLUTION

Hello Himadri,

You can do this in a JavaScript policy. Something like the below:

var content = context.getVariable("response.content") //read the response
content = JSON.parse(content); //parse into an object
if (content.speed > 50) { //apply the condition
    content.speed_class = "high";
}
else {
    content.speed_class = "low";
}
context.setVariable("response.content", JSON.stringify(content)); //set it back on the response

Reference: https://docs.apigee.com/api-platform/reference/policies/javascript-policy

Thanks,

Girish

View solution in original post

1 REPLY 1

Hello Himadri,

You can do this in a JavaScript policy. Something like the below:

var content = context.getVariable("response.content") //read the response
content = JSON.parse(content); //parse into an object
if (content.speed > 50) { //apply the condition
    content.speed_class = "high";
}
else {
    content.speed_class = "low";
}
context.setVariable("response.content", JSON.stringify(content)); //set it back on the response

Reference: https://docs.apigee.com/api-platform/reference/policies/javascript-policy

Thanks,

Girish