Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.8.0, 2.9.0] Aggregation function attempted to calculate value using empty array #996

Open
anatoly-spb opened this issue Jan 31, 2024 · 0 comments

Comments

@anatoly-spb
Copy link

anatoly-spb commented Jan 31, 2024

Hello!

I have a simple json:

{
 "array": [
   {
    "count": 1
   },
   {
    "count": 2
   },
   {
    "count": 3
   }
 ]
}

Is it possible to use aggregate in the following case:

	@Autowired
	com.jayway.jsonpath.Configuration jsonPathConfiguration;
	
	@Test
	void parseJson() {
	  Object object = jsonPathConfiguration.jsonProvider().parse("{\n"
	      + "      \"array\": [ \n"
	      + "       {\n"
	      + "           \"count\": 1\n"
	      + "       },\n"
	      + "       {\n"
	      + "           \"count\": 2\n"
	      + "       },\n"
	      + "       {\n"
	      + "           \"count\": 3\n"
	      + "       }\n"
	      + "       ]\n"
	      + "   }");
	  assertThat(object).isNotNull().isInstanceOf(Map.class);
	  JsonPath countSum = JsonPath.compile("$.array[*].count.sum()");
	  countSum.read(object);
	}

?

Now I got the exception:

com.jayway.jsonpath.JsonPathException: Aggregation function attempted to calculate value using empty array
	at com.jayway.jsonpath.internal.function.numeric.AbstractAggregation.invoke(AbstractAggregation.java:59)
	at com.jayway.jsonpath.internal.path.FunctionPathToken.evaluate(FunctionPathToken.java:41)
	at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:90)
	at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:80)
	at com.jayway.jsonpath.internal.path.PathToken.handleArrayIndex(PathToken.java:143)
	at com.jayway.jsonpath.internal.path.WildcardPathToken.evaluate(WildcardPathToken.java:42)
	at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:90)
	at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:80)
	at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:66)
	at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:99)
	at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:107)
	at com.jayway.jsonpath.JsonPath.read(JsonPath.java:179)
	at com.jayway.jsonpath.JsonPath.read(JsonPath.java:153)

But there is a work around:

Object counts = JsonPath.read(object, "$.array[*].count");
Object countSum = JsonPath.read(counts, "$.sum()"); // 6.0(Double)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant