Using jsonpath to get parent node

隐身守侯 提交于 2019-12-01 22:32:48

问题


Using node JSONPath, how can I get the parent node name from child node value

{
  "store": {
    "book": [
      {
        "id":"1",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "id":"2",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      }
    ]
  }
}

I use this expression to identify the child node based on value, I want to use this child node to find the parent node

$.[?(@.id =="1")]

回答1:


You do not specify which implementation of JSON Path, but with both Gatling (Scala) and JayWay (Java) you can use nested filters to filter by children while returning the parent, grandparent or whatever. Here is a sample:

With this JSON:

{
  "a": {
    "b": {
        "c": {
            "d": {
                "e": "foo"
            }
        },
        "something": "bar"
    }
  }
}

And this path:

$.a.b[?(@.c[?(@.d[?(@.e == "foo")])])].something

Returns:

[ "bar" ]

I am able to retrieve b by using expressions to get to lower nodes as the filter.

Some other implementations error out on these expressions.




回答2:


Unfortunately, JSONpath does not support search by parameters in the contents of a child object: http://goessner.net/articles/JsonPath/



来源:https://stackoverflow.com/questions/41878757/using-jsonpath-to-get-parent-node

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