如何使用jsonpath算法?
How to use jsonpath-ng arithmetic?
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)
这会引发错误:
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 333, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\ply\yacc.py", line 1063, in parseopt_notrack
lookahead = get_token() # Get the next token
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 179, in token
return next(self.iterator)
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\lexer.py", line 35, in tokenize
t = new_lexer.token()
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\ply\lex.py", line 386, in token
newtok = self.lexerrorf(tok)
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\lexer.py", line 167, 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.lexer.JsonPathLexerError: Error on line 1, col 17: Unexpected character: +
我错过了什么吗? (我使用的是最新版本:1.5.2)
您需要使用扩展解析器才能使其工作:
#from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parser
jsonpath_expr = parser.parse('$.objects[*].cow + $.objects[*].cat')
obj = {'objects': [
{'cow': 2, 'cat': 3},
{'cow': 4, 'cat': 6}
]}
print([match.value for match in jsonpath_expr.find(obj)])
这将打印:[5, 10]
。所以它实际上是在每个 'row'.
上添加每头牛和猫的值
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)
这会引发错误:
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 333, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\ply\yacc.py", line 1063, in parseopt_notrack
lookahead = get_token() # Get the next token
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\parser.py", line 179, in token
return next(self.iterator)
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\lexer.py", line 35, in tokenize
t = new_lexer.token()
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\ply\lex.py", line 386, in token
newtok = self.lexerrorf(tok)
File "C:\Users\micha\AppData\Roaming\Python\Python38\site-packages\jsonpath_ng\lexer.py", line 167, 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.lexer.JsonPathLexerError: Error on line 1, col 17: Unexpected character: +
我错过了什么吗? (我使用的是最新版本:1.5.2)
您需要使用扩展解析器才能使其工作:
#from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parser
jsonpath_expr = parser.parse('$.objects[*].cow + $.objects[*].cat')
obj = {'objects': [
{'cow': 2, 'cat': 3},
{'cow': 4, 'cat': 6}
]}
print([match.value for match in jsonpath_expr.find(obj)])
这将打印:[5, 10]
。所以它实际上是在每个 'row'.