{"record":{"id":"311595df903c7d2d","repo":"json-path/JsonPath","slug":"aggregation-function-attempted-to-calculate-value","errorCode":null,"errorMessage":"Aggregation function attempted to calculate value using empty array","messagePattern":"Aggregation function attempted to calculate value using empty array","errorType":"exception","errorClass":"JsonPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java","lineNumber":59,"sourceCode":"            Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);\n            for (Object obj : objects) {\n                if (obj instanceof Number) {\n                    Number value = (Number) obj;\n                    count++;\n                    next(value);\n                }\n            }\n        }\n        if (parameters != null) {\n            for (Number value : Parameter.toList(Number.class, ctx, parameters)) {\n                count++;\n                next(value);\n            }\n        }\n        if (count != 0) {\n            return getValue();\n        }\n        throw new JsonPathException(\"Aggregation function attempted to calculate value using empty array\");\n    }\n}\n","sourceCodeStart":41,"sourceCodeEnd":62,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java#L41-L62","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check the array is non-empty before running the aggregation, or supply a default in application code.","Use Option.SUPPRESS_EXCEPTIONS or Option.DEFAULT_PATH_LEAF_TO_NULL if appropriate and handle the null/default result.","Catch JsonPathException and return a sensible default (0, null) for empty inputs.","Restructure the query to first filter non-empty documents, e.g. $[?(@.items.length() > 0)].items.sum()."],"exampleFix":"// before\nDouble avg = JsonPath.read(json, \"$.orders.avg\"); // throws when orders is []\n// after\nList<Double> a = JsonPath.read(json, \"$.orders\");\nDouble avg = (a == null || a.isEmpty()) ? 0.0 : JsonPath.read(json, \"$.orders.avg\");","handlingStrategy":"validation","validationCode":"List<?> arr = JsonPath.read(json, \"$.items\");\nDouble avg = (arr == null || arr.isEmpty()) ? 0.0 : JsonPath.read(json, \"$.items.avg\");","typeGuard":null,"tryCatchPattern":"try {\n    return JsonPath.read(json, \"$.items.avg\");\n} catch (JsonPathException e) {\n    if (e.getMessage().contains(\"empty array\")) return 0.0; // default for empty input\n    throw e;\n}","preventionTips":["Never assume aggregated collections are non-empty","Guard with a length() pre-check or catch JsonPathException","Return explicit defaults (0/null) for empty aggregates in API layers"],"tags":["json-path","aggregation","empty-collection"],"backgroundTag":"empty-result-set","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}