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

jsonpath filter not recognized #116

Open
VBobCat opened this issue Dec 30, 2022 · 0 comments
Open

jsonpath filter not recognized #116

VBobCat opened this issue Dec 30, 2022 · 0 comments

Comments

@VBobCat
Copy link

VBobCat commented Dec 30, 2022

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

  • expression: $..[successes,errors]..?(typeof @ ==='string')
  • input data:
    [
        {
      	"type": "rpc",
      	"tid": 62,
      	"action": "Lembrete",
      	"method": "createLembrete",
      	"result": {
      		"0": {
      			"success": true,
      			"successes": [
      				["Message1", ["Message2"], "Message3", ["Message4", ["Message5"]]]
      			]
      
      		},
      		"records": [{
      			"id": 3257823
      		}],
      		"success": true
      	}
        }
    ]
  • result (working as desired):
    [
        "Message1",
        "Message3",
        "Message2",
        "Message4",
        "Message5"
    ]
    

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?

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

1 participant