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

jsonpath filter not recognized #116

Open VBobCat opened 1 year ago

VBobCat commented 1 year ago

I've composed and tested this expression in jsonpath.com:

However, when running the python equivalent below (Python 3.10.9, jsonpath-ng==1.5.3):

import json
from jsonpath_ng.ext import parse
from jsonpath_ng.jsonpath import JSONPath, DatumInContext

EXPRESSION = r"$..[successes,errors]..?(typeof @ ==='string')"

INPUT = r"""
[
    {
      "type": "rpc",
      "tid": 62,
      "action": "Lembrete",
      "method": "createLembrete",
      "result": {
          "0": {
              "success": true,
              "successes": [
                  ["Message1", ["Message2"], "Message3", ["Message4", ["Message5"]]]
              ]

          },
          "records": [{
              "id": 3257823
          }],
          "success": true
      }
    }
]
"""

if __name__=='__main__':
    data = json.loads(INPUT)
    parsed_jsonpath: JSONPath = parse(EXPRESSION)
    jsonpath_results: list[DatumInContext] = parsed_jsonpath.find(data)
    print([datum_in_context.value for datum_in_context in jsonpath_results])

I get the following exception:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2022.1.3\plugins\python\helpers\pydev\pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2022.1.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\project\extras\test_jsonpath.py", line 33, in <module>
    parsed_jsonpath: JSONPath = parse(EXPRESSION)
  File "C:\project\venv\lib\site-packages\jsonpath_ng\ext\parser.py", line 172, in parse
    return ExtentedJsonPathParser(debug=debug).parse(path)
  File "C:\project\venv\lib\site-packages\jsonpath_ng\parser.py", line 44, in parse
    return self.parse_token_stream(lexer.tokenize(string))
  File "C:\project\venv\lib\site-packages\jsonpath_ng\parser.py", line 67, in parse_token_stream
    return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
  File "C:\project\venv\lib\site-packages\ply\yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "C:\project\venv\lib\site-packages\ply\yacc.py", line 1201, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "C:\project\venv\lib\site-packages\ply\yacc.py", line 192, in call_errorfunc
    r = errorfunc(token)
  File "C:\project\venv\lib\site-packages\jsonpath_ng\parser.py", line 81, in p_error
    raise JsonPathParserError('Parse error at %s:%s near token %s (%s)'
jsonpath_ng.exceptions.JsonPathParserError: Parse error at 1:23 near token ? (?)
python-BaseException

If jsonpath_ng doesn't support the expression $..[successes,errors]..?(typeof @ ==='string') as given, what should I do in order to achieve the same result?