Click or drag to resize
Json.NETQuerying JSON with JSONPath
 

This sample loads JSON and then queries values from it using SelectToken(String) with a JSONPath query.

Sample
Usage
 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