h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
564 stars 85 forks source link

jsonpath_ng.exceptions.JsonPathLexerError - Unexpected character: ? #141

Closed FlanniganColum closed 9 months ago

FlanniganColum commented 9 months ago

Hey. So i am trying out the jsonpath_ng for the first time and by using the below sample code i am getting the below error. while using the site "https://www.javainuse.com/jsonpath" i am validating the jsonPath syntax and it works fine, do i need to escape the question mark or is this a known issue, and if so do you know a workaround

Package Version


jsonpath-ng 1.6.0

os="Linux"
data = module.zabbix.get_service(value=os, filter="name")
jsonpath_expression = parse("$.[*].parents[*][?(@.name == 'Sample')].serviceid")
for match in jsonpath_expression.find(data):
print(match.value)
Traceback (most recent call last):
  File "./loop.py", line 81, in <module>
    sys.exit(main(sys.argv[1:]))
  File "./loop.py", line 75, in main
    jsonpath_expression = parse('$.[*].parents[*][?(@.name == "Sample")].serviceid')
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/parser.py", line 22, in parse
    return JsonPathParser().parse(string)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/parser.py", line 45, in parse
    return self.parse_token_stream(lexer.tokenize(string))
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/parser.py", line 69, in parse_token_stream
    return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/ply/yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/ply/yacc.py", line 1063, in parseopt_notrack
    lookahead = get_token()     # Get the next token
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/parser.py", line 195, in token
    return next(self.iterator)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/lexer.py", line 33, in tokenize
    t = new_lexer.token()
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/ply/lex.py", line 386, in token
    newtok = self.lexerrorf(tok)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/lexer.py", line 166, 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 17: Unexpected character: ?
FlanniganColum commented 9 months ago

After reading more i noticed i made a mistake and was not using the .ext, so i have fixed that and now the error i get is. Course the ultimate plan is to have the path name lookup a variable so i can using it dynamically.

Traceback (most recent call last):
  File "./loop.py", line 85, in <module>
    sys.exit(main(sys.argv[1:]))
  File "./loop.py", line 76, in main
    jsonpath_expression = parse("$.[*]parents[?(@.name == 'Sample')].name")
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/ext/parser.py", line 172, in parse
    return ExtentedJsonPathParser(debug=debug).parse(path)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/parser.py", line 45, in parse
    return self.parse_token_stream(lexer.tokenize(string))
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/parser.py", line 69, in parse_token_stream
    return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/ply/yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/ply/yacc.py", line 1201, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/ply/yacc.py", line 192, in call_errorfunc
    r = errorfunc(token)
  File "/opt/apps/zabbix/zbxSLA/.venv/lib64/python3.6/site-packages/jsonpath_ng/parser.py", line 84, in p_error
    % (t.lineno, t.col, t.value, t.type))
jsonpath_ng.exceptions.JsonPathParserError: Parse error at 1:5 near token parents (ID)
FlanniganColum commented 9 months ago

Actually i found the solution if i adjusted the syntax to the below it worked for me

jsonpath_expression = parse("$.[*]['parents'][?(@.name == 'Sample')].['name']")