[{ "language-version": "", "languages-supported": "English", "owning-region": "Corporate", "peak-user-count": "", "product-department": "Risk and Compliance Management", "product-owner": "Johnson Paul", "product-owner-cc": "05444", "product-sponsor": "Dooley, Peter", "product-sponsor-cc": "05444", "product-sponsor-domain": " IT Security", "soa-classification": "service provider", "software-version": "6.2", "supported-environment": "Dev/Staging, Production", "supported-region": "Global", "unix-windows": "Windows" }, { "language-version": "", "languages-supported": "English", "owning-region": "Corporate", "peak-user-count": "", "product-department": "Enterprise Architecture", "product-owner": "Brian D. Smith", "product-owner-cc": "05431", "product-sponsor": "Cynthia Czabala", "product-sponsor-cc": "", "product-sponsor-domain": "Enterprise Information Solutions and Customer Loyalty Marketing Technologies", "soa-classification": "service provider", "software-version": "2", "supported-environment": "Staging, Int, Production", "supported-region": "Corporate", "unix-windows": "Windows" }]
Solved! Go to Solution.
Assuming your json is in a variable called the_json_payload, and that you want to extract all the elements where soa-classification matches "service provider", you can use an Extract Variables policy like the following
<ExtractVariables name="ExtractVariablesFromJSON"> <Source>the_json_payload</Source> <JSONPayload> <Variable name="matchingElements" type="float"> <JSONPath>$[*][?(@.soa-classification == 'service provider')]</JSONPath> </Variable> </JSONPayload> </ExtractVariables>
This will extract all the matching elements to a variable called matchingElements.
For a complete reference of the policy, see https://docs.apigee.com/api-services/reference/extract-variables-policy.
Also, this tutorial and online JSONPath evaluator help when trying to figure out how to deal with JSONPath.
If you want to conditionally extract based on the value of a query parameter, simply replace the hardcoded 'service provider' in my previous example with the corresponding query parameter (surrounded with brackets):
<ExtractVariables async="false" continueOnError="false" enabled="true" name="MatchingElementsFromJSON"> <DisplayName>MatchingElementsFromJSON</DisplayName> <Source>the_json_payload</Source> <JSONPayload> <Variable name="matchingElements"> <JSONPath>$[*][?(@.soa-classification == {request.queryparam.soa_classification})]</JSONPath> </Variable> </JSONPayload> </ExtractVariables>
The attached proxy uses this policy in conjunction with a dummy assign variable (to mimic your file read) and returns the matching objects extractfromjsonbasedonqueryparam-rev1-2018-02-08.zip
[ { "language-version": "", "languages-supported": "English", "owning-region": "Corporate", "peak-user-count": "", "product-department": "Risk and Compliance Management", "product-owner": "Johnson Paul", "product-owner-cc": "05444", "product-sponsor": "Dooley, Peter", "product-sponsor-cc": "05444", "product-sponsor-domain": " IT Security", "soa-classification": "service provider", "software-version": "6.2", "supported-environment": "Dev/Staging, Production", "supported-region": "Global", "unix-windows": "Windows" }, { "language-version": "", "languages-supported": "English", "owning-region": "Corporate", "peak-user-count": "", "product-department": "Enterprise Architecture", "product-owner": "Brian D. Smith", "product-owner-cc": "05431", "product-sponsor": "Cynthia Czabala", "product-sponsor-cc": "", "product-sponsor-domain": "Enterprise Information Solutions and Customer Loyalty Marketing Technologies", "soa-classification": "service provider", "software-version": "2", "supported-environment": "Staging, Int, Production", "supported-region": "Corporate", "unix-windows": "Windows" } ]
Assuming your json is in a variable called the_json_payload, and that you want to extract all the elements where soa-classification matches "service provider", you can use an Extract Variables policy like the following
<ExtractVariables name="ExtractVariablesFromJSON"> <Source>the_json_payload</Source> <JSONPayload> <Variable name="matchingElements" type="float"> <JSONPath>$[*][?(@.soa-classification == 'service provider')]</JSONPath> </Variable> </JSONPayload> </ExtractVariables>
This will extract all the matching elements to a variable called matchingElements.
For a complete reference of the policy, see https://docs.apigee.com/api-services/reference/extract-variables-policy.
Also, this tutorial and online JSONPath evaluator help when trying to figure out how to deal with JSONPath.
@deboraelkin
Thanks!
I want to set this field as a query param and depending upon what value I am sending,I just want to extract that chunk of data.
For example,soa-classification can be "service provider","service consumer" or "api client".I am using java script to read the json file.
So I want to set this as query param and I want to check for that value using javascript and extract data.
Please can you help me on this?
@Vidisha, what I'm trying to point out, is that you don't need javascript to achieve that. Just use the ExtractVariables policy configuration I mentioned above. It'll return all the elements in the array where soa-classification is equal to the value provided in your query parameter.
Do review the sample proxy I attached, it does exactly that.
If you want to conditionally extract based on the value of a query parameter, simply replace the hardcoded 'service provider' in my previous example with the corresponding query parameter (surrounded with brackets):
<ExtractVariables async="false" continueOnError="false" enabled="true" name="MatchingElementsFromJSON"> <DisplayName>MatchingElementsFromJSON</DisplayName> <Source>the_json_payload</Source> <JSONPayload> <Variable name="matchingElements"> <JSONPath>$[*][?(@.soa-classification == {request.queryparam.soa_classification})]</JSONPath> </Variable> </JSONPayload> </ExtractVariables>
The attached proxy uses this policy in conjunction with a dummy assign variable (to mimic your file read) and returns the matching objects extractfromjsonbasedonqueryparam-rev1-2018-02-08.zip
Hey thanks!
what if I want to extract data based on just a single word which I will send as a query param and want to extract all arrays that contain that word?
for example: searchtext=staging
I want to get all arrays as response that contain this word in it
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |