Skip to content Skip to sidebar Skip to footer

How To Use Jsonpath-ng Arithmetic?

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 jsonpat

Solution 1:

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

#from jsonpath_ng import jsonpath
from jsonpath_ng.extimport parser

jsonpath_expr = parser.parse('$.objects[*].cow + $.objects[*].cat')
obj = {'objects': [
    {'cow': 2, 'cat': 3},
    {'cow': 4, 'cat': 6}
]}
print([match.valuefor match in jsonpath_expr.find(obj)])

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

Post a Comment for "How To Use Jsonpath-ng Arithmetic?"