Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

jsonpath-ng package claims to support basic arithmetic (https://pypi.org/project/jsonpath-ng/), but the parser won't accept arithmetic statements. Here is one of them:

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)

which throws an error:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    jsonpath_expr = parse('$.objects[*].cow + $.objects[*].cat')
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesjsonpath_ngparser.py", line 14, in parse
    return JsonPathParser().parse(string)
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesjsonpath_ngparser.py", line 32, in parse
    return self.parse_token_stream(lexer.tokenize(string))
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesjsonpath_ngparser.py", line 55, in parse_token_stream
    return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesplyyacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesplyyacc.py", line 1063, in parseopt_notrack
    lookahead = get_token()     # Get the next token
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesjsonpath_ngparser.py", line 179, in token
    return next(self.iterator)
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesjsonpath_nglexer.py", line 35, in tokenize
    t = new_lexer.token()
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesplylex.py", line 386, in token
    newtok = self.lexerrorf(tok)
  File "C:UsersmichaAppDataRoamingPythonPython38site-packagesjsonpath_nglexer.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: +

Am I missing something? (I'm using the newest version: 1.5.2)

question from:https://stackoverflow.com/questions/65943012/how-to-use-jsonpath-ng-arithmetic

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
167 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to use the extended parser to make it work:

#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)])

This prints: [5, 10]. So it's actually adding each cow and cat value on each 'row'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...