{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
0
Question by miloud · Apr 16, 2015 at 09:51 AM · 8.8k Views api proxyedgeextractvariablesjsonjson payload

Extract variables from json using filter expression

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

Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

4 Answers

  • Sort: 
avatar image
0
Best Answer

Answer by miloud · Apr 16, 2015 at 01:05 PM

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.

Comment
Add comment Show 3 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sudheendras ♦   · Apr 16, 2015 at 01:33 PM 0
Link

Awesome! Thanks!.

avatar image Dino ♦♦   · Apr 16, 2015 at 04:43 PM 0
Link

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.

avatar image miloud Dino ♦♦ · Apr 17, 2015 at 11:15 AM 0
Link

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.

avatar image
0

Answer by sudheendras   · Apr 16, 2015 at 10:33 AM

Try

    <JSONPayload>
        <Variable name="mylicense">
            <JSONPath>$.licenses[?(@.uri == 'somelicense')].status</JSONPath>
        </Variable>
    </JSONPayload>
Comment
Add comment · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by miloud · Apr 16, 2015 at 11:09 AM

@sudheendra1

Thanks for your reply! I already tried that, unfortunately it didn't work,

Thanks

Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sudheendras ♦   · Apr 16, 2015 at 11:15 AM 0
Link

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>
screen-shot-2015-04-16-at-44214-pm.png (35.2 kB)
avatar image
0

Answer by Dino   · Apr 16, 2015 at 05:37 PM

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

curl -i http://yourhostname/test2975/test1


test2975.zip (5.5 kB)
Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image miloud · Apr 17, 2015 at 11:17 AM 0
Link

yes you are right, this is working for me

<JSONPath>$.licenses[?(@.uri == 'somelicense')].status[0]</JSONPath>

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I transform a JSON Object from target server response into a different JSON object to send to the client? 1 Answer

How to customize Edge's default 400 error message? 1 Answer

How to remove few fields from JSON array response? 2 Answers

How do I send JSON body to my POST request in Javascript and same way do do I get JSON response in my APIGEE api proxy program 2 Answers

Can Apigee Edge be used to proxy requests from internal systems to external services? 1 Answer

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges