json-path/JsonPath · error · JsonPathException

Aggregation function attempted to calculate value using empt

Error message

Aggregation function attempted to calculate value using empty array

What it means

Numeric aggregation functions (sum, avg, min, max, stddev) iterate the target array and require at least one value (count != 0). If the array is empty (or yields no numeric values), AbstractAggregation.invoke throws JsonPathException('Aggregation function attempted to calculate value using empty array') because there is no meaningful aggregate of zero elements.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java:59

            Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);
            for (Object obj : objects) {
                if (obj instanceof Number) {
                    Number value = (Number) obj;
                    count++;
                    next(value);
                }
            }
        }
        if (parameters != null) {
            for (Number value : Parameter.toList(Number.class, ctx, parameters)) {
                count++;
                next(value);
            }
        }
        if (count != 0) {
            return getValue();
        }
        throw new JsonPathException("Aggregation function attempted to calculate value using empty array");
    }
}

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Check the array is non-empty before running the aggregation, or supply a default in application code.
  2. Use Option.SUPPRESS_EXCEPTIONS or Option.DEFAULT_PATH_LEAF_TO_NULL if appropriate and handle the null/default result.
  3. Catch JsonPathException and return a sensible default (0, null) for empty inputs.
  4. Restructure the query to first filter non-empty documents, e.g. $[?(@.items.length() > 0)].items.sum().

Example fix

// before
Double avg = JsonPath.read(json, "$.orders.avg"); // throws when orders is []
// after
List<Double> a = JsonPath.read(json, "$.orders");
Double avg = (a == null || a.isEmpty()) ? 0.0 : JsonPath.read(json, "$.orders.avg");
Defensive patterns

Strategy: validation

Validate before calling

List<?> arr = JsonPath.read(json, "$.items");
Double avg = (arr == null || arr.isEmpty()) ? 0.0 : JsonPath.read(json, "$.items.avg");

Try / catch

try {
    return JsonPath.read(json, "$.items.avg");
} catch (JsonPathException e) {
    if (e.getMessage().contains("empty array")) return 0.0; // default for empty input
    throw e;
}

Prevention

When it happens

Trigger: Calling $.items.sum(), $.items.avg(), etc. via JsonPath.read() when the items array is empty ([]) or missing values so that no values are aggregated (count stays 0).

Common situations: Aggregating over API response collections that are legitimately empty (no records today), aggregating a filtered path where the filter matches nothing, or running aggregations during integration tests with empty fixtures.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/311595df903c7d2d. Report an issue: GitHub.