标签: jsonpath-ng

如何使用 jsonpath-ng 算术?

jsonpath-ng包声称支持基本算术(https://pypi.org/project/jsonpath-ng/),但解析器不会接受算术语句。这是其中之一:

from jsonpath_ng import parse

jsonpath_expr = parse('$.objects[*].cow + $.objects[*].cat')
obj = {'objects': [
  {'cow': 2, 'cat': 3},
  {'cow': 4, 'cat': 6}
]}
values = [match.value for match in jsonpath_expr.find(obj)]  
print(values)
Run Code Online (Sandbox Code Playgroud)

这会引发错误:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    jsonpath_expr = parse('$.objects[*].cow + $.objects[*].cat')
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 14, in parse
    return JsonPathParser().parse(string)
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 32, in parse
    return self.parse_token_stream(lexer.tokenize(string))
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 55, in parse_token_stream
    return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
  File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\ply\yacc.py", line …
Run Code Online (Sandbox Code Playgroud)

python jsonpath jsonpath-ng

4
推荐指数
1
解决办法
671
查看次数

标签 统计

jsonpath ×1

jsonpath-ng ×1

python ×1