6

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?

syntagma
24.6k17 gold badges85 silver badges142 bronze badges
asked Oct 15, 2018 at 12:47
2
  • I don't have experience with this but it could be sum(price) Commented Oct 15, 2018 at 12:49
  • Have you reviewed the tests for usage examples?: github.com/json-path/JsonPath/blob/… Commented Oct 15, 2018 at 12:51

3 Answers 3

7

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.

answered Oct 15, 2018 at 13:22
Sign up to request clarification or add additional context in comments.

1 Comment

Ah I see, thanks for that. And the example also only works with the jayway implementation, so Im afraid there will be no way to use functions in the way I need to ..
1

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
 }
 }
}
answered May 7, 2020 at 16:13

2 Comments

It would be possible to use a filter condition when using min/max like the following: "$.max($.store.book[?(@.price < 10)].price)" ?
@IoTuser Yes actually you can play around the same jsonpath.herokuapp.com here
0

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();
...
answered May 7, 2022 at 10:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.