I am trying to implement what seems like a simple JSON path filter, but failing to get it working. Wondering if others more experience with Json.NET's implementation of JSON path have ideas on next steps.
This scenario fails but I think should work?
var jsonText = @"{
'event': {
'data': {
'intField': 1,
'stringField': 'hello'
}
}
}";
JObject json = JsonConvert.DeserializeObject<JObject>(jsonText);
string jsonPath = "$.event.data[?(@.intField == 1)]";
IList<JToken> output = json.SelectTokens(jsonPath).ToList();
// this check fails
Assert.IsTrue(output.ToList().Count > 0);
If I massage the JSON payload by adding a dummy array around the 'data' object, then I can get the query working. However, I would rather not massage the JSON payload.
var jsonText = @"{
'event': {
'data': [{
'intField': 1,
'stringField': 'hello'
}]
}
}";
JObject json = JsonConvert.DeserializeObject<JObject>(jsonText);
string jsonPath = "$.event.data[?(@.intField == 1)]";
IList<JToken> output = json.SelectTokens(jsonPath).ToList();
// now this works
Assert.IsTrue(output.ToList().Count > 0);