Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Need JSONPath help... Extract Variables not working as expected :-(

So close yet so far... I can return the resourceIds (extracted.StreamIds) but not a filtered list of "where isActive==true" (extracted.activeStreamIds).

This is grabbing a response from a ServiceCallout. Do I need to explicitly set the response to be application/json and if so, how would I do that?

<JSONPayload>
<Variable name="extracted.activeStreamIds">
<JSONPath>$.[?(@.isActive==true)].resourceId</JSONPath>
</Variable>
<Variable name="extracted.StreamIds">
<JSONPath>$..resourceId</JSONPath>
</Variable>
</JSONPayload>

Thanks!

Solved Solved
0 13 1,434
2 ACCEPTED SOLUTIONS

hmm ok I've looked a little further.

using this example JSON

[
    {
      "isActive" : "false",
      "resourceId" : 1
    },
    {
      "isActive" : "false",
      "resourceId" : 2
    },
    {
      "isActive" : "true",
      "resourceId" : 3
    }
]


I have found that this jsonpath query works:

$[?(@.isActive == 'true')].resourceId

It returns an array with a single element, 3.

But using a json with boolean values instead of string values, like this:

[
    {
      "isActive" : false,
      "resourceId" : 1
    },
    {
      "isActive" : false,
      "resourceId" : 2
    },
    {
      "isActive" : true,
      "resourceId" : 3
    }
]

I could not find a query that worked as desired.

This does not work:

$[?(@.isActive == true)].resourceId

This query checks for the existence of the isActive property, not for its value:

$[?(@.isActive)].resourceId

...and yields [1,2,3], which I think is not what you want.

I think this is a limitation of the v0.8.0 jayway JSON Path library currently used by Apigee.

If I try this query

$[?(@.isActive == true)].resourceId

...with v2.4.0 of jayway, against a JSON file that uses boolean literals and not strings, it works as expected, returns [3] .

But you can't ask to use v2.4.0 of jayway in ExtractVariables. It is currently locked to v0.8.0.

There are other people who have asked for an upgrade to a current version of jsonpath, but for now it's not possible. The ticket that tracks the request to allow a more modern jsonpath is b/132486339 .

The workarounds available to you as I see them:

  1. use a JS-based jsonpath, (import a jsonpath JS module and do it in a JS callout)
  2. use a Java callout that takes advantage of a more recent jsonpath library, like this one: https://github.com/DinoChiesa/ApigeeEdge-Java-JSONPath (I built this to address similar limitations for other customers)
  3. pre-process your JSON to replace all boolean literals with strings, and THEN use the ExtractVariables with the string fiilter syntax
  4. transform the JSON to XML and use xpath

View solution in original post

Glad to help, Peter!

View solution in original post

13 REPLIES 13