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

Wish: '$.select.from.object[*($.a.list.of.ids)]' and '$.select.from.variable[$VARIABLE]' #117

Closed
drpicox opened this issue Mar 17, 2020 · 1 comment
Labels

Comments

@drpicox
Copy link

drpicox commented Mar 17, 2020

This is not an issue is a wish. If I find the time, may someday I will implement it by myself.

The idea is the following: Imagine that we have the following JSON (source: https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape/ )

{
    posts : {
        byId : {
            "post1" : {
                id : "post1",
                author : "user1",
                body : "......",
                comments : ["comment1", "comment2"]
            }, ...
        },
        allIds : ["post1", "post2"]
    },
    comments : {
        byId : {
            "comment1" : {
                id : "comment1",
                author : "user2",
                comment : ".....",
            }, ...
        },
        allIds : ["comment1", "comment2", "comment3", "commment4", "comment5"]
    },
    users : { ... }
}

What if I want all the comments of the post1?

JSONPath({path: '$.comments.byId[*($.posts.byId[post1].comments)]', json });

What if I want a post given a JS variable and do not worry about escaping?

JSONPath({path: '$.posts.byId[$postId]', json }, {postId: 'post1'});

And #108 is also nice.

Thanks! I'll check JSONata.

@brettz9
Copy link
Collaborator

brettz9 commented Jun 28, 2020

Regarding new proposals, please see #124. Closing as any proposals for significant new features should I think be first drafted as part of this standardization process and then an issue can be filed here to match. However, feel free to comment further as needed.

Note there is currently the sandbox feature for something similar and this can accept callbacks which can be passed context info, e.g., this test case from test/test.eval.js:

         it('sandbox (with parsing function)', () => {
            const expected = [json.store.book];
            const result = jsonpath({
                json,
                sandbox: {
                    filter (arg) {
                        return arg.category === 'reference';
                    }
                },
                path: "$..[?(filter(@))]", wrap: false
            });
            assert.deepEqual(result, expected);
        });

But your callback would have to make its own jsonpath call and then filter accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants