According to JsonPath on GitHub it shall be possible to access max(), min() or the sum() of an array but I dont know how. With this exampledata:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
I would expect it to work like
$..book.length
so im trying
$..price.sum
but that didn't do the job.
Can someone help me?
-
I don't have experience with this but it could be sum(price)crodev– crodev2018年10月15日 12:49:25 +00:00Commented Oct 15, 2018 at 12:49
-
Have you reviewed the tests for usage examples?: github.com/json-path/JsonPath/blob/…Royal Wares– Royal Wares2018年10月15日 12:51:02 +00:00Commented Oct 15, 2018 at 12:51
3 Answers 3
From the docs:
Functions can be invoked at the tail end of a path - the input to a function is the output of the path expression
Given this statement, you might expect the example JSON you supplied above to work with an expression such as $.store..price.max() but this expression does not work, instead it throws an error:
Aggregation function attempted to calculate value using empty array
You can read more about that in this JsonPath issue on GitHub.
Meanwhile, here's an example which does work.
Given the following JSON:
{
"price": [
1.0,
2.0
]
}
The JsonPath functions work as follows:
$..price.min()returns[1.0]$..price.max()returns[2.0]$..price.sum()returns[3.0]
The above have been verified using the online evaluator.
1 Comment
You may try the following :-
For calculating SUM
$.sum($.store.book[0].price,$.store.book[1].price,$.store.book[2].price,$.store.book[3].price)
For min and max
$.max($.store.book[0].price,$.store.book[1].price,$.store.book[2].price,$.store.book[3].price)
$.min($.store.book[0].price,$.store.book[1].price,$.store.book[2].price,$.store.book[3].price)
With this example data:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
2 Comments
"$.max($.store.book[?(@.price < 10)].price)" ?Your JSON data contains a set of records and you need to perform aggregation on these sets. The example code uses a sum operation, and similar way of handling is for both min and max. Yet JSONPath only supports aggregation on a simple set and enumeration before aggregation, but it does not support aggregation on a set of records.
You can use SPL, an open-source Java package, to do it if you want a simple and easy alternative. You just need a single line of code:
| A | |
|---|---|
| 1 | =json(file("data.json").read()).store.book.sum(price) |
SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as sum.splx and invoke it in a Java application as you call a stored procedure:
...
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call sum()");
st.execute();
...