Search code examples
jsonpathjson-path-expression

How to use multiple fields to evaluate a condition using JSONPath expresstion


I have a json of type:

[{
    "event": "click",
    "status": "success",
    "test": true

}
,
  
{
    "event": "send",
    "status": "success",
    "test": true
}
,
  
{
    "event": "checked",
    "status": "failure",
    "test": false
}]

I want to check if the event click was "success".

I am new to jsonpath expressions and I have tried doing this so far:

$.[?(@.event === 'click')].status

The above expression returns : [ success ]

but I am trying to do 'if an event named 'click' has a status 'success' then return true.


Solution

  • JSON Path is a query syntax, not a logic processor. What you've done is correct. You'll need to write custom code to verify that [ "success" ] has been returned.

    Alternatively, you can update your path to $.[?(@.event == 'click' && @.status == 'success')] and then check to see if the resulting array has any items.

    Note that support for this does depend on the implementation you're using, as not all implementations support the same operations and syntaxes.