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

Exception: Parse error at 1:4 near token / (SORT_DIRECTION) #57

Open cvancar opened 4 years ago

cvancar commented 4 years ago

Hello,

I am getting the exception while execution following example:

import json import jsonpath_rw_ext text = '{"name/subname": "value"}' json = json.loads(text) matches = jsonpath_rw_ext.match("name/subname", json)

How to escape the character '/', please?

Thank you. Petr

alberto56 commented 3 years ago

I ran into a similar issue; wrapping the search terms in single quotes worked for me:

from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse
data = [{"test" : "hello"}, {"test" : "a/b"}]
print(parse("$[?(@.test=hello)]").find(data)[0].value)
# {'test': 'hello'}
print(parse("$[?(@.test='a/b')]").find(data)[0].value)
# {'test': 'a/b'}
data2 = {"a" : "b", "c/d" : "e"}
print(parse("$.a").find(data2)[0].value)
# b
print(parse("$.'c/d'").find(data2)[0].value)
# e
michaelmior commented 1 year ago

It would be great if there was an escape function in the library that would properly escape any special characters so they could be used as a component of a path.