Hi All, I am facing an issue to Extract variables with JSONPath on a ExtractVariables policy using filter expression: The ExtractVariables policy:
<ExtractVariables name="Extract-License"> <DisplayName>Extract my License</DisplayName> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> <Source>refresponse</Source> <VariablePrefix>refvars</VariablePrefix> <JSONPayload> <Variable name="mylicense"> <JSONPath>$.licenses[?(@.uri = 'somelicense')].status</JSONPath> </Variable> </JSONPayload> </ExtractVariables>
This get executed after a successful ServiceCallout that returns :
{ "result": "ok", "message": "licenses", "licenses": [{ "uri": "http://0-www.crossref.org.libcat.lafayette.edu/tdm_license", "status": "accepted", "reviewed_at": "2013-07-02T14:52:06+00:00" }, { "uri": "somelicense", "status": "accepted", "reviewed_at": "2013-07-02T14:52:06+00:00" }] }
If I replace the JSONPath expression with
$.licenses[1].status
... it works as expected.
What's wrong with the first expression? Thanks for your help
Solved! Go to Solution.
We finally managed to get it working, this happens only if we use VariablePrefix, when I removed it worked fine. (@dzuluaga the the application/json header was already set in the response)
Thanks you all for you help.
Try
<JSONPayload> <Variable name="mylicense"> <JSONPath>$.licenses[?(@.uri == 'somelicense')].status</JSONPath> </Variable> </JSONPayload>
Interesting! I see it working in my personal org. Here is the screen shot of trace page -
What's the Content type of your response? It should be "application/json".
Here is my Extract Variable policy -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1"> <DisplayName>Extract Variables 1</DisplayName> <Source>response</Source> <JSONPayload> <Variable name="mylicense"> <JSONPath>$.licenses[?(@.uri == 'somelicense')].status</JSONPath> </Variable> </JSONPayload> </ExtractVariables>
I've seen this happening when the Content-Type header in the response is set to something different than "application/json". To fix it, try setting Content-Type header to "application/json" in an AssignMessage policy before extract variable policy gets executed.
We finally managed to get it working, this happens only if we use VariablePrefix, when I removed it worked fine. (@dzuluaga the the application/json header was already set in the response)
Thanks you all for you help.
Awesome! Thanks!.
Sorry, can you explain what you mean by "this happens only if we use VariablePrefix" ? What happens, exactly? I'm not clear on the original problem either - are you saying the original JSONPath expression is not working? What result do you see?
And in this comment, are you saying the ExtractVariables policy works differently depending on whether VariablePrefix element is present? Does the ExtractVariables not work properly if you use VariablePrefix? I'd like to hear more, please.
Yes that's right , in ExtractVariables if I use VariablePrefix it does not work, but if I remove it, I am able to see the variable and use it.
test2975.zipIf you use this JSONPath expression:
$.licenses[?(@.uri == 'somelicense')].status
You will get an array in return. The filter in JSONPath returns all nodes for which the filter returns true. So this is an array, of zero or more elements.
If you are certain that the array has at least one element, and that the first element is the one of interest, then you can use this:
$.licenses[?(@.uri == 'somelicense')][0].status
This will give you a single string. In my tests, it returns "accepted".
This works with or without the VariablePrefix element in the ExtractVariables policy.
I've attached a full apiproxy bundle that demonstrates this. Deploy it to an environment and then invoke it with
yes you are right, this is working for me
<JSONPath>$.licenses[?(@.uri == 'somelicense')].status[0]</JSONPath>
User | Count |
---|---|
3 | |
2 | |
1 | |
1 | |
1 |