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

I'm having an error trying to create a JSONPath - Exception: Parse error at 1: 4 near token? (?) #75

Closed kespineira closed 2 weeks ago

kespineira commented 3 years ago

Hi!

I am trying to create a filter on the following JSON but for some reason I am getting the error all the time:

Exception: Parse error at 1: 4 near token? (?)

This is my code:

from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse

r = requests.get(url, headers=json.loads(headers))
j = r.text
j_exp = parse("$..[?(@name == 'CCAA')].idAssetType")
id = j_exp.find(j)
print(id)

Confirm that the value of j is:

[{"idAssetType":6,"name":"CCAA","enterprise":{"id":1,"name":"APV"}}]

nivesnine commented 3 years ago

remove the ".." after the "$"

j_exp = parse("$[?(@.name == 'CCAA')].idAssetType")

and if you only want the idAssetType:

print(id[0].value)

michaelmior commented 2 weeks ago

This seems to be resolved, so closing. If there are still any problems, feel free to reopen.