JsonPath expression not applying the condition

48 Views Asked by At

I'm recieving multiple Events (following JSON) from a Queue and I don't want to take all of them, only the events with Sales Organization="180C":

{
    "eventID": "string",
    "eventType": "string",
    "eventDate": "2024-03-15T18:21:24Z",
    "source": "string",
    "dataKey": "string",
    "dataChange": {
        "KEY": []
    },
    "dataScope": {
        "dataObject": "string",
        "KEY": [
            {
                "scope": "scope1",
                "value": "scope2"
            },
            {
                "scope": "scope2",
                "value": "scope3"
            },
            {
                "scope": "Sales Organization",
                "value": "190B" 
            }
        ]
    }
}

I used this filter but not working as intended :

$.[?($.dataScope.KEY[?(@.scope=='Sales Organization' && @.value=='190B')])]

When I try this filter alone : $.dataScope.KEY[?(@.scope=='Sales Organization' && @.value=='190B')] I get an empty array [] when the condition is not satisfied.

1

There are 1 best solutions below

4
gregsdennis On

UDPATE:

  1. When using the brackets syntax [], don't use a dot .. So you'll start with $[, not $.[.
  2. When iterating over items, you'll need to use the @ to start paths in filters. @ indicates the current item root. $ indicates the document root.

So, instead of starting your path with $.[?($.dataScope, start with $[?(@.dataScope.

$[?(@.dataScope.KEY[?(@.scope=='Sales Organization' && @.value=='190B')])]

Previous answer, not knowing that the posted event was one in an array of events.

$.[?($.dataScope

What is this for? Why the extra bit on the front?

You have the right path, it's just buried in this extra filter for some reason. Just use

$.dataScope.KEY[?(@.scope=='Sales Organization' && @.value=='190B')]

(also, $.[] with a dot is invalid. Omit the dot when using brackets: $[].)

You can test this at https://json-everything.net/json-path, which implements the new IETF JSON Path specification RFC 9535.


EDIT

The output for this (from my playground) is

[
  {
    "Value": {
      "scope": "Sales Organization",
      "value": "190B"
    },
    "Location": "$['dataScope']['KEY'][2]"
  }
]