How do I get a deeply-nested property from JSON string?

[*]

You can use JToken.SelectTokens() for this purpose. It allows for querying JSON using wildcards and recursive searches using the JSONPath syntax:

var root = JToken.Parse(json);
var myThings = root.SelectTokens("..myThings[*]").ToList();

Here ".." is the recursive descent operator and "myThings[*]" means to return all array items of the property "myThings".

Prototype fiddle.

If the array entries of "myThings[*]" correspond to some POCO MyThing, you can use JToken.ToObject<T>() to deserialize them after querying:

    var myThings = root.SelectTokens("..myThings[*]").Select(t => t.ToObject<MyThing>()).ToList();

Leave a Comment