Querying JSON with complex JSON Path
April 6, 2024 ยท View on GitHub
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.";
}