Click or drag to resize
Json.NET

Throw error when JSON Path not found

 

This sample loads JSON and then queries values from it using SelectToken. An error is thrown when part of the JSON path is not found.

Sample
Usage
JArray items = JArray.Parse(@"[
  {
    'Name': 'John Doe',
  },
  {
    'Name': 'Jane Doe',
  }
]");

// A true value for errorWhenNoMatch will result in an error if the queried value is missing 
string result;
try
{
    result = (string)items.SelectToken(@"$.[3]['Name']", errorWhenNoMatch: true);
}
catch (JsonException)
{
    result = "Unable to find result in JSON.";
}