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
Same invoke() flow as the sequence functions' index error: if the object list is empty, there is nothing to select and the function throws JsonPathException('Aggregation function attempted to calculate value using empty array'). This branch fires before any index math when objectList has no elements.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java:40
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);
List<Object> objectList = new ArrayList<>();
objects.forEach(objectList::add);
int targetIndex = this.targetIndex(ctx, parameters);
if (targetIndex >= 0) {
return objectList.get(targetIndex);
} else {
int realIndex = objectList.size() + targetIndex;
if (realIndex > 0) {
return objectList.get(realIndex);
} else {
throw new JsonPathException("Target index:" + targetIndex + " larger than object count:" + objectList.size());
}
}
}
throw new JsonPathException("Aggregation function attempted to calculate value using empty array");
}
protected int getIndexFromParameters(EvaluationContext ctx, List<Parameter> parameters) {
List<Number> numbers = Parameter.toList(Number.class, ctx, parameters);
return numbers.get(0).intValue();
}
}
View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Check the array is non-empty before invoking first()/last()/index().
- Catch JsonPathException and return null/default instead of the first element.
- Prefer application-side access (list.get(0)) with an isEmpty() guard for clearer control flow.
- Use Option.SUPPRESS_EXCEPTIONS where the library supports suppressing such errors.
Example fix
// before Object first = JsonPath.read(json, "$.results.first()"); // results = [] // after List<Object> r = JsonPath.read(json, "$.results"); Object first = (r != null && !r.isEmpty()) ? r.get(0) : null;
Defensive patterns
Strategy: validation
Validate before calling
List<?> r = JsonPath.read(json, "$.results"); Object first = (r != null && !r.isEmpty()) ? r.get(0) : null;
Try / catch
try {
return JsonPath.read(json, "$.results.first()");
} catch (JsonPathException e) {
if (e.getMessage().contains("empty array")) return null;
throw e;
} Prevention
- Guard first()/last() calls with isEmpty() checks
- Default to null rather than throwing when collections may be empty
- Use SUPPRESS_EXCEPTIONS where acceptable and null-check results
When it happens
Trigger: Calling first(), last(), index(n), or slice() on an empty array, e.g. $.items.first() where items is [], via JsonPath.read()/compile().
Common situations: Taking 'the first' element of collections that can legitimately be empty (no search results, no notifications), and empty fixture data in tests.
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
- Aggregation function attempted to calculate value using empt
- Could not find matching close quote for %s when parsing : %s
- Could not find matching close for %s when parsing regex in :
- Expected character: %c
- No results for Key %s found in map!
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/c3271260473342ce.
Report an issue: GitHub.