问题
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