HI,
I have a ServiceCallout policy which returns a JSON object as a response in a variable called otpresponse.
The response looks like this.
{"sessiontoken":"776f1f1b311ddec7","counter":309998532595415141}
I want to access these variables individually in the Javascript policy.
I tried using this but in vain
context.setVariable("otp", context.getVariable('otpresponse.content.sessiontoken'));
How can I fetch the variables individually in a Javascript?
Solved! Go to Solution.
I thought that the JSON elements cannot be extracted directly within a JS using context.getVariable(). We can alternatively use Extract variable policy and extract using Jsonpath.
Here is what I did when i needed JSON element in JS - var payload = context.getVariable("otpresponse.content"); var jsonObject= JSON.parse(payload); var otp = jsonObject.sessiontoken;
@apigee1234567 , It should work out of the box. Please use trace console to debug same.
Hello @Anil Sagar
Thanks for your reply. It is not working.
If I write ontext.setVariable("otp", context.getVariable('otpresponse.content'));, I get the entire response in JSON form.
But I require to fetch individual values in the JSON, which I am not when I write otpresponse.content.sessiontoken
I thought that the JSON elements cannot be extracted directly within a JS using context.getVariable(). We can alternatively use Extract variable policy and extract using Jsonpath.
Here is what I did when i needed JSON element in JS - var payload = context.getVariable("otpresponse.content"); var jsonObject= JSON.parse(payload); var otp = jsonObject.sessiontoken;
Hello @Abhishek Subramanya
Bingo!! Yes that is what I was looking for precisely. Thanks a lot.
Thank you @Abhishek Subramanya 🙂