Yogster
Yogster

Reputation: 914

Selecting an ancestor up the hierarchy using JSON.Net / JSONPath

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

Answers (1)

Brian Rogers
Brian Rogers

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

Related Questions