Getting parent of matched element with jsonpath

末鹿安然 提交于 2019-12-02 03:59:27

问题


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:


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



来源:https://stackoverflow.com/questions/31226500/getting-parent-of-matched-element-with-jsonpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!