Closed SouvikDcoder closed 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
Cool, thanks @christianhennen. Will try it out.
Closing since it seems like there is a solution here. Feel free to reopen if there's still a problem.
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.
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](https://user-images.githubusercontent.com/18427983/194651661-e25ab91e-4e27-4ad9-8278-7da3647078cc.gif)
Getting traceback when trying to execute the code in my IDE :![jsonpath_code](https://user-images.githubusercontent.com/18427983/194651647-2eb61885-5ec0-440a-885a-25f70acf2285.gif)