Getting parent of matched element with jsonpath

2019-02-27 00:11发布

Let's say I have a JSON object such as this:

[{
    name: "A",
    sub: [
        {prop: "1"},
        {prop: "2"},
        {prop: "3"},
    ]
},
{
    name: "B",
    sub: [
        {prop: "7"},
        {prop: "8"},
        {prop: "9"},
    ]    
}];

How can I get the parent of the element who's prop value is 2?

jsonpath.query(data, "$..[?(@.prop==2)]");

This will return the object itself, but I don't know how to get it's parent.

1条回答
家丑人穷心不美
2楼-- · 2019-02-27 01:04

I ended up using parent instead of query method. As simple as that. I just missed it when I was reading the docs.

查看更多
登录 后发表回答