Click or drag to resize
Json.NETQuerying JSON with SelectToken
 

SelectToken provides a method to query LINQ to JSON using a single string path to a desired JToken. SelectToken makes dynamic queries easy because the entire query is defined in a string.

SelectToken

SelectToken is a method on JToken and takes a string path to a child token. SelectToken returns the child token or a null reference if a token couldn't be found at the path's location.

The path is made up of property names and array indexes separated by periods, e.g. Manufacturers[0].Name.

SelectToken Example
 1JObject o = JObject.Parse(@"{
 2  'Stores': [
 3    'Lambton Quay',
 4    'Willis Street'
 5  ],
 6  'Manufacturers': [
 7    {
 8      'Name': 'Acme Co',
 9      'Products': [
10        {
11          'Name': 'Anvil',
12          'Price': 50
13        }
14      ]
15    },
16    {
17      'Name': 'Contoso',
18      'Products': [
19        {
20          'Name': 'Elbow Grease',
21          'Price': 99.95
22        },
23        {
24          'Name': 'Headlight Fluid',
25          'Price': 4
26        }
27      ]
28    }
29  ]
30}");
31
32string name = (string)o.SelectToken("Manufacturers[0].Name");
33// Acme Co
34
35decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
36// 50
37
38string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
39// Elbow Grease
SelectToken with JSONPath

SelectToken supports JSONPath queries. Find out more about JSONPath here.

SelectToken With JSONPath
 1JObject o = JObject.Parse(@"{
 2  'Stores': [
 3    'Lambton Quay',
 4    'Willis Street'
 5  ],
 6  'Manufacturers': [
 7    {
 8      'Name': 'Acme Co',
 9      'Products': [
10        {
11          'Name': 'Anvil',
12          'Price': 50
13        }
14      ]
15    },
16    {
17      'Name': 'Contoso',
18      'Products': [
19        {
20          'Name': 'Elbow Grease',
21          'Price': 99.95
22        },
23        {
24          'Name': 'Headlight Fluid',
25          'Price': 4
26        }
27      ]
28    }
29  ]
30}");
31
32// manufacturer with the name 'Acme Co'
33JToken acme = o.SelectToken("$.Manufacturers[?(@.Name == 'Acme Co')]");
34
35Console.WriteLine(acme);
36// { "Name": "Acme Co", Products: [{ "Name": "Anvil", "Price": 50 }] }
37
38// name of all products priced 50 and above
39IEnumerable<JToken> pricyProducts = o.SelectTokens("$..Products[?(@.Price >= 50)].Name");
40
41foreach (JToken item in pricyProducts)
42{
43    Console.WriteLine(item);
44}
45// Anvil
46// Elbow Grease
SelectToken with LINQ

SelectToken can be used in combination with standard LINQ methods.

SelectToken With LINQ Example
 1IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
 2// Lambton Quay
 3// Willis Street
 4
 5IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
 6// null
 7// Headlight Fluid
 8
 9decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
10// 149.95
See Also