Skip to content

Grouping doesn't apply as expected when filtering #312

Description

@daanp

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions