Skip to content

Latest commit

 

History

History
32 lines (29 loc) · 947 Bytes

ErrorWhenNoMatchQuery.md

File metadata and controls

32 lines (29 loc) · 947 Bytes

Querying JSON with complex JSON Path

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

var 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.";
}

snippet source | anchor