IBM / JSONata4Java

Open Source Java version of JSONata
Apache License 2.0
91 stars 38 forks source link

Grouping doesn't apply as expected when filtering #312

Open daanp opened 3 months ago

daanp commented 3 months ago

Apologies in advance if overlooking something obvious - I am not well versed in JSONata

I'm trying to reproduce this case with the library: https://try.jsonata.org/ZkhsVPArq

It seems that there is something weird happening when applying a grouping after a filter. An example can be found in the test below:

    @Test
    void testBrokenPredicate() throws Exception {
        JSONObject json = new JSONObject("{\"vendor\":[{\"name\":\"kwik\",\"number\":1},{\"name\":\"kwek\",\"number\":2},{\"name\":\"kwak\",\"number\":3},{\"name\":\"kwak\",\"number\":4}]}");

        @Nonnull final JsonNode jsonObj;
        try {
            jsonObj = new ObjectMapper().readTree(json.toString());
        } catch (IOException e) {
            throw new APIException(e, "JSONata failed during reading of json");
        }
        Expressions jsonataExpression = Expressions.parse("vendor[name=\"kwak\"]{`name`: `number`}");
        Expressions brokenJsonataExpression = Expressions.parse("vendor[name=\"kwek\"]{`name`: `number`}");

        JsonNode result = jsonataExpression.evaluate(jsonObj);
        JsonNode brokenResult = brokenJsonataExpression.evaluate(jsonObj);

        // The brokenResult does not apply the grouping as expected
        JSONAssert.assertEquals("{\"kwak\":[3,4]}", result.toString(), true);

        // Here we can see the actual result
        JSONAssert.assertEquals("{\"kwek\":{\"number\":2,\"name\":\"kwek\"}}", brokenResult.toString(), true);

        // I expect this to succeed as seen here https://try.jsonata.org/ZkhsVPArq, but it fails
        JSONAssert.assertEquals("{\"kwek\":2}", brokenResult.toString(), true);
    }

Please let me know what I'm doing wrong or if this is a known issue.