h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
572 stars 85 forks source link

Filter does not behave correctly #120

Open BenjaminPelletier opened 1 year ago

BenjaminPelletier commented 1 year ago

I want to find "Things" that descend from my parent structure regardless of where they are. "Things" are characterized by containing a thing_type field. To accomplish this, I use the JSONPath $..*[?(@.thing_type)]. jsonpath.com confirms that this works with the sample data below (one match found, as expected):

{
    "name": "Thing 1",
    "thing_type": "type_a",
    "children": [
        {
            "thing": {
                "name": "Thing 2",
                "thing_type": "type_b"
            }
        }
    ]
}

However, jsonpath-ng 1.5.3 fails to find this match:

import jsonpath_ng.ext
path = '$..*[?(@.thing_type)]'
v = {
    "name": "Thing 1",
    "thing_type": "type_a",
    "children": [
        {
            "thing": {
                "name": "Thing 2",
                "thing_type": "type_b"
            }
        }
    ]
}
matches = jsonpath_ng.ext.parse(path).find(v)
print(len(matches))

The above code prints 0 instead of the expected 1.

BenjaminPelletier commented 1 year ago

The bridgecrewio fork does not have this bug; perhaps this repo has been abandoned.

michaelmior commented 11 months ago

This doesn't appear to be a bug to me. $..*[?(@.thing_type)] refers to any element of an array which contains an object with the property thing_type. In the case of the example data, thing_type is a nested property.

The path $..*[?(@..thing_type)] (note the extra dot) correctly returns a match.