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

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

Closed
SouvikDcoder opened this issue Oct 7, 2022 · 3 comments

Comments

@SouvikDcoder
Copy link

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

@SouvikDcoder SouvikDcoder changed the title Getting error for Unexpected character: ? while trying to evaluate a jsonpath Getting error Unexpected character: ? while trying to evaluate a jsonpath Oct 7, 2022
@christianhennen
Copy link

christianhennen commented Oct 26, 2022

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

Cool, thanks @christianhennen. Will try it out.

@michaelmior
Copy link
Collaborator

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

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

3 participants