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

Getting error `Unexpected character: ?` while trying to evaluate a jsonpath #113

Closed SouvikDcoder closed 1 year ago

SouvikDcoder commented 1 year ago

I am trying to incorporate the below jsonpath to fetch an _id value from a json object based on a provided company name. While I test it out on the URL, I am getting my desired result. However, once I try to execute and parse the same jsonpath in my IDE, I am getting a traceback.

jsonpath : $[?(@.company=="GADTRON")]._id

PFA, the sample.txt file that contains the json object where I am trying the combinations.

I am also attaching a gif of the trials on the URL and the code traceback when it is executed.

Please Note : ? is a valid character literal among the jsonpath expressions, as mentioned in the example below: $.objects[?some_field = "foobar")]

Traceback :

--------------------------------------------------- Traceback (most recent call last): File "/workspace/ComplexFileParserPython/src/main.py", line 38, in <module> jsonpath_expr = parse('$[?(@.company=="GADTRON")]._id') File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/jsonpath_ng/parser.py", line 21, in parse return JsonPathParser().parse(string) File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/jsonpath_ng/parser.py", line 44, in parse return self.parse_token_stream(lexer.tokenize(string)) File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/jsonpath_ng/parser.py", line 67, in parse_token_stream return new_parser.parse(lexer = IteratorToTokenStream(token_iterator)) File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/ply/yacc.py", line 333, in parse return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc) File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/ply/yacc.py", line 1063, in parseopt_notrack lookahead = get_token() # Get the next token File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/jsonpath_ng/parser.py", line 193, in token return next(self.iterator) File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/jsonpath_ng/lexer.py", line 33, in tokenize t = new_lexer.token() File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/ply/lex.py", line 386, in token newtok = self.lexerrorf(tok) File "/workspace/.pyenv_mirror/user/current/lib/python3.8/site-packages/jsonpath_ng/lexer.py", line 165, in t_error raise JsonPathLexerError('Error on line %s, col %s: Unexpected character: %s ' % (t.lexer.lineno, t.lexpos - t.lexer.latest_newline, t.value[0])) jsonpath_ng.exceptions.JsonPathLexerError: Error on line 1, col 2: Unexpected character: ?

sample.txt'

successfully fetched the desired value on the URL : jsonpath_online

Getting traceback when trying to execute the code in my IDE : jsonpath_code

christianhennen commented 1 year ago

Had the same problem today. Fixed it with a comment from #8 : "change your import statements to use parse from the extensions"

import json
from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse
SouvikDcoder commented 1 year ago

Cool, thanks @christianhennen. Will try it out.

michaelmior commented 1 year ago

Closing since it seems like there is a solution here. Feel free to reopen if there's still a problem.