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")]
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.
With JayWay it works but the query provided by Jayson is wrong, because if you change the value to "foo2" it will still return "bar".
This can be tested on http://jsonpath.herokuapp.com/.
Query:
$.a.b[?(@.c.d.e=="foo")].something
Returns:
["bar"]
Query:
$.a.b[?(@.c.d.e=="foo2")].something
Returns:
[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With