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

Grouping doesn't apply as expected when filtering #312

Open
daanp opened this issue Jun 21, 2024 · 0 comments
Open

Grouping doesn't apply as expected when filtering #312

daanp opened this issue Jun 21, 2024 · 0 comments

Comments

@daanp
Copy link

daanp commented Jun 21, 2024

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.

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