Backend Development 6 min read

JsonPath Functions for Array Operations: min, max, avg, stddev, length, sum

This article demonstrates how to use JsonPath's built‑in functions such as $.ss.min(), $.ss.max(), $.ss.avg(), $.ss.stddev(), $.ss.length() and $.ss.sum() to compute minimum, maximum, average, standard deviation, length and sum of JSON array elements, providing Java code examples and expected console output.

FunTester
FunTester
FunTester
JsonPath Functions for Array Operations: min, max, avg, stddev, length, sum

The article continues the JsonPath practice series by introducing the function API that operates on JSON arrays, complementing the previously covered basic JsonPath queries.

Sample JSON data used in the examples:

JSONObject json = JSON.parseObject("{" +
        "    \"store\": {" +
        "        \"book\": [" +
        "            {" +
        "                \"category\": \"reference\"," +
        "                \"author\": \"Nigel Rees\"," +
        "                \"title\": \"Sayings of the Century\"," +
        "                \"page\": \"D\"," +
        "                \"pages\": [\"S\",\"X\",\"G\"]," +
        "                \"price\": 8.95" +
        "            }," +
        "            {" +
        "                \"category\": \"fiction\"," +
        "                \"author\": \"Evelyn Waugh\"," +
        "                \"title\": \"Sword of Honour\"," +
        "                \"page\": \"A\"," +
        "                \"pages\": [\"A\",\"B\"]," +
        "                \"price\": 12.99" +
        "            }," +
        "            {" +
        "                \"category\": \"fiction\"," +
        "                \"author\": \"Herman Melville\"," +
        "                \"title\": \"Moby Dick\"," +
        "                \"isbn\": \"0-553-21311-3\"," +
        "                \"page\": \"B\"," +
        "                \"pages\": [\"E\",\"F\"]," +
        "                \"price\": 8.99" +
        "            }," +
        "            {" +
        "                \"category\": \"fiction\"," +
        "                \"author\": \"J. R. R. Tolkien\"," +
        "                \"title\": \"The Lord of the Rings\"," +
        "                \"isbn\": \"0-395-19395-8\"," +
        "                \"page\": \"C\"," +
        "                \"pages\": [\"C\",\"D\"]," +
        "                \"price\": 22.99" +
        "            }" +
        "        ]," +
        "        \"bicycle\": {" +
        "            \"color\": \"red\"," +
        "            \"price\": 19.95" +
        "        }" +
        "    }," +
        "    \"expensive\": 10," +
        "    \"ss\": [32,32,4,23]" +
        "}");

Getting the Minimum Value of an Array

JsonPath expression: $.ss.min()

Object read = JsonPath.read(json, "$.ss.min()");
output(read);

Console output shows 4.0 (a double value).

Getting the Maximum Value of an Array

JsonPath expression: $.ss.max()

Object read = JsonPath.read(json, "$.ss.max()");
output(read);

Console output shows 32.0 (also a double ).

Getting the Average Value of an Array

JsonPath expression: $.ss.avg()

Object read = JsonPath.read(json, "$.ss.avg()");
output(read);

Console output shows 22.75 (a double ).

Getting the Standard Deviation of an Array

JsonPath expression: $.ss.stddev()

Object read = JsonPath.read(json, "$.ss.stddev()");
output(read);

Console output shows 11.431863365173676 (a double ).

Getting the Length of an Array

JsonPath expressions: $.ss.length() and $.store.book.length()

Object read = JsonPath.read(json, "$.ss.length()");
output(read);

Object read2 = JsonPath.read(json, "$.store.book.length()");
output(read2);

Console output shows 4 for the ss array.

Getting the Sum of an Array

JsonPath expression: $.ss.sum()

Object read = JsonPath.read(json, "$.ss.sum()");
output(read);

Console output shows 91.0 (a double ).

The series of JsonPath API tutorials is now complete; the author mentions upcoming content on a JsonPath utility using Groovy operator overloading.

backendJavaJSONJsonPatharray-functions
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.