2

I want to access a nested value with JSON.NET. I know I can use the .SelectToken() method to access a nested value (see for example this question or this question). My issue is that the JSON I'm trying to access has keys with dots in them:

var json = @"
{
  ""data.dot"": {
    ""value"": 5,
  }
}";

var jo = JObject.Parse(json);
Console.WriteLine(jo.SelectToken("data.dot.value")); // <-- doesn't work
Métoule
  • 13,062
  • 2
  • 56
  • 84

1 Answers1

6

I found the answer while writing this question, so I might as well share my findings.

It turns out that the .SelectToken method is very powerful, and:

So in my case, I could write:

jo.SelectToken("['data.dot'].value"); // escaped property
jo.SelectToken("$..value"); // complex JSON path

I could also use the JToken indexer, but contrary to the .SelectToken method, it would throw an exception if the JSON doesn't contain the data.dot key:

jo["data.dot"]["value"]
Métoule
  • 13,062
  • 2
  • 56
  • 84