Reputation: 914
Using JSON.Net / JSONPath, is there a way to go up the JSON object hierarchy, other than calling JToken.Parent
multiple times?
For example, for the following JSON:
{
"grandparent" : {
"parent" : {
"child" : {
"property" : "value"
}
}
}
}
Selecting the contents of "child" is easy enough:
var theChild = theJson.SelectToken("$.grandparent.parent.child");
But if I now want to select the contents of "grandparent" from "child", the only way I can figure out to do it is by calling:
var theGrandparent = theChild.Parent.Parent.Parent;
Which seems a bit clumsy. Is there another way of doing this?
Upvotes: 1
Views: 1942
Reputation: 129787
No. There is no provision in JSONPath for selecting a parent node from a child. See http://goessner.net/articles/JsonPath. Therefore you will need to use the .Parent
property on the JToken
like you are already doing.
Upvotes: 3