JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
975 stars 175 forks source link

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

Closed drpicox closed 4 years ago

drpicox commented 4 years ago

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 commented 4 years ago

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.