Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter does not behave correctly #120

Open
BenjaminPelletier opened this issue Mar 30, 2023 · 2 comments
Open

Filter does not behave correctly #120

BenjaminPelletier opened this issue Mar 30, 2023 · 2 comments

Comments

@BenjaminPelletier
Copy link

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
Copy link
Author

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

@michaelmior
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants