What I tried to do is get the maximum price value using the max() function.
com.jayway.jsonpath.JsonPathException: Aggregation function attempted to calculate value using empty array
at com.jayway.jsonpath.internal.function.numeric.AbstractAggregation.invoke(AbstractAggregation.java:62)
at com.jayway.jsonpath.internal.path.FunctionPathToken.evaluate(FunctionPathToken.java:38)
at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
at com.jayway.jsonpath.internal.path.PathToken.handleArrayIndex(PathToken.java:133)
at com.jayway.jsonpath.internal.path.WildcardPathToken.evaluate(WildcardPathToken.java:40)
at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:181)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:164)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:151)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:502)
Is this a bug or is this type of function usage not yet supported?
I'm using the latest from 'master'.
You are trying to apply the function max() to the result of the evaluation. That is not how things work. The function will be applied to each object matching the path. In your example max will be invoked on each price separately. I guess
price` is a number so your path is not correct.
I understand what you are trying to do but there is currently no support for running functions over a result set.
Yes, the 'price' is a number. The queries above are based on the example JSON document in this project's README file.
What would it take to support running functions over a result set? Can you give some pointers in the code where I could start looking into adding this feature?
The pull request, #197, is fully backwards compatible. All unit tests work. It allows you to use functions on evaluated arrays (result sets) rather than just static arrays.
See the usecases in ResultSetFunctionTest that use the example JSON document from the README file.
I think that based on the documentation in the README, most people expect that if this works:
$.max($.store.book[0].price, $.store.book[1].price, $.store.book[2].price, $.store.book[3].price)
then this should also:
$.max($.store.book[*].price)
Same here, the possibility of this working would be super handy
$.sum($.products[?(@category = "categoryA")].value)
$.products[?(@category = "categoryA")].value.sum()
for the object
"products": [
"category": "categoryA",
"value": 157.12
"category": "categoryA",
"value": 11.99
"category": "categoryB",
"value": 55.99
@pwaksman973 - This does work. Your query is malformed.
Try one of these on your object:
$.sum($..products[?(@.category == 'categoryA')].value)
$..products[?(@.category == 'categoryA')].value.sum()
I think that based on the documentation in the README, most people expect that if this works: $.max($.store.book[0].price, $.store.book[1].price, $.store.book[2].price, $.store.book[3].price)
then this should also: $.max($.store.book[*].price)
thinks for u help
We are trying to upgrade from json-path 2.3 version to 2.7 and run into this issue.
Following is the JSON
"response": {
"dates": ["2016-04-19T00:00:00",
"2016-04-19T01:00:00",
"2016-04-19T02:00:00",
"2016-04-19T03:00:00",
"2016-04-19T04:00:00",
"2016-04-19T05:00:00",
"2016-04-19T06:00:00",
"2016-04-19T07:00:00",
"2016-04-19T08:00:00"],
"data": [1,2,3,4,5,6,7,8.1999999999999318,9.019999999999981]
Running $..data.min()
gives following Exception
Caused by: com.jayway.jsonpath.JsonPathException: Aggregation function attempted to calculate value using empty array
at com.jayway.jsonpath.internal.function.numeric.AbstractAggregation.invoke(AbstractAggregation.java:59) ~[json-path-2.7.0.jar:2.7.0]
at com.jayway.jsonpath.internal.path.FunctionPathToken.evaluate(FunctionPathToken.java:40) ~[json-path-2.7.0.jar:2.7.0]
at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:66) ~[json-path-2.7.0.jar:2.7.0]
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:99) ~[json-path-2.7.0.jar:2.7.0]
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:107) ~[json-path-2.7.0.jar:2.7.0]
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:179) ~[json-path-2.7.0.jar:2.7.0]
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:88) ~[json-path-2.7.0.jar:2.7.0]
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:77) ~[json-path-2.7.0.jar:2.7.0]