h2non / jsonpath-ng

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

Cannot find items in data structure #174

Closed shahargli closed 1 month ago

shahargli commented 1 month ago

Hello,

I am trying to use jsonpath_ng.ext to find items in a json data structure. Following is the program, where I expected to find one item (panels.datasource.type=='type1'), but find() always returns empty list

from jsonpath_ng.ext import parse

data = {
    'panels': [
        {
            'datasource': {'type': 'type1'},
        },
        {
            'datasource': {'type': 'type2'},
        }
    ]
}
jsonpath_expr = parse('panels.datasource[?(@.type=="type1")]')
found = jsonpath_expr.find(data)
print(found)
michaelmior commented 1 month ago

Your syntax is not quite right. The filter syntax, that is [?…], operates on arrays. If you try instead panels[?datasource.type = "type1"], you'll get {"datasource": {"type": "type1"} in your results.

michaelmior commented 1 month ago

I'm going to close this, but feel free to reopen if you still have questions.