In Apigee Edge JSONPath Logical operator are not working

thamilbecse
Participant II

Hi Team,

I am using Apigee Edge Public Cloud. Not able to execute below JSONPath Expression

{
    "quotas": {
        "quota": [
            {
                "appname": "A",
                "operation": "ManageOrder",
                "value": "1000"
            },
            {
                "appname": "B",
                "operation": "ManageOrder",
                "value": "1000"
            }
        ]
    }
}

Below expression not working giving empty result []

$.quotas.quota[?(@.appname=='Belong') and ?(@.operation=='ManageOrder')].value[0]

Kindly check and let me know do we have logical operator (AND-&& or OR-||) support in Apigee Edge for JSONPath

1 9 663
9 REPLIES 9

Where exactly are you using that JSONPath ?

Can you provide more context please?

Is it being used in a policy, if so, which one? Provide the entire policy configuration.

I tried out your JSON Path in the AssignMessage/AssignVariable policy, and here's what I found.

  1. I think the jsonpath syntax you want is something like this:
    $.quotas.quota[?(@.appname=='B' && @.operation=='ManageOrder')].value

    It replaces the "and" with a double ampersand and collapses the two question marks into just one. This works for me with your sample data on this online jsonpath evaluator. (Note: the online evaluator is not a Google property).

    It returns an array of values, of length 1. I think that is not quite what you want.

  2. The syntax like this:

    $.quotas.quota[?(@.appname=='B' && @.operation=='ManageOrder')].value[0]

    ...did not work for me in the online evaluator. I am not sure why this is. The definitive documentation on the JSONPath syntax seems to be a bit lacking, so I am unsure if it is valid.

    Even so, it might be the online evaluator that is doing something incorrect. For whatever reason, I could not get it to work.

    [After further review, I learned that while this syntax seems understandable, it is not valid. See this issue for an ongoing discussion. Many jsonpath users would like this to be valid, but that kind of query is not included in the specification. A query with a filter expression followed by a property name and then an array indexer.... will not work in the AssignVariable/jsonpath function.]

  3. the compound logical predicate that I described as "correct" above ...

    ?(@.appname=='B' && @.operation=='ManageOrder')

    ....appears to not be handled correctly by the underlying jsonpath library used within Apigee Edge.

    The jsonpath feature depends on a somewhat older library, which I think was created before the use of compound logical statements was described or broadly supported.

    I think you could easily select what you want with a JS step.

    <Javascript name='JS-1' >
      <Source><![CDATA[
        var $ = JSON.parse(context.getVariable('json_blob'));
        var value = $.quotas.quota.filter( function(e) { return e.appname=='B' && e.operation=='ManageOrder'; } )[0].value;
        context.setVariable('value',value);
      ]]></Source>
    </Javascript>
    	

Hi

Thanks for your reply. Could you please try the same xpath in extract variables policy. It is throwing error when we mention "&&". it is not accepted in XML.

In an XML document, ampersands are special and must be encoded. This is not limited to use within Apigee Edge; it applies to all XML documents, anywhere.

You need to wrap the ampersands in a CDATA section, or encode them as

& amp; & amp ;

(collapse spaces in the above in order to get it to work)

Hi Dino,

Could you please share me sample project.. I have tried this too in extract variable policy.. but still it is not working .

I'll be glad to help but you will need to please give me some more information about what you are having trouble with. What is the XML document? What XPath are you trying? What results do you see? What results do you expect to see?

Hi Dino,

Please find the requested information below:

Request:

{"quotas":{"quota":[{"appname":"A","operation":"MO","value":"100"},{"appname":"B","operation":"PO","value":"200"}]}}

I need a JSONPath to use in Extract Variable Policy:

$.quotas.quota[?(@.appname=='B'& & @.operation=='PO')].value[0]

I have tried below Xpath which is not working in Extract variable policy

Expect Result: 200

Could you please help me by providing the valid Xpath

I think there is a misunderstanding somewhere. Either I am misunderstanding the question or you are misunderstanding the use of XPath.

XPath can be used to query XML.

JSONPath can be used to query JSON.

I think you are asking what XPath can you use to query the JSON. And the answer to that is: None.

If I have misunderstood, please clarify.

Hi Dino,

Yes I need JSONXpath to query the JSON.

When I try to use below expression I am able to see some response:

$.quotas.quota[?(@.appname=='B')].value[0]

But for the below expression I am getting empty response only

$.quotas.quota[?(@.appname=='B'&&@.operation=='MO')].value[0]

Could you please tell me why logical operators are not wokring in JSON (&& - || AND/OR)