I'm trying to write a filtering class in C# using Newtonsoft.Json for some JSON documents which has the requirement that we can filter against anything in the JSON document and validate that a particular document satisfies the condition or not. So far, it seems like JSONPath works really nicely for checking if a value exists in a set of objects, but I'm struggling to figure out if I can validate against a single property, such as:
{
"outerObject": {
"innerObject": {
"property": "value"
}
}
}
So far I've been able to validate that the property exists with: $.outerObject.innerObject.property, but what I need to do is validate that $.outerObject.innerObject.property equals value. This led me to trying $.outerObject.innerObject.property?(@ == 'value'), but I'm getting an exception about an unexpected @. Next I tried $.outerObject.innerObject.property?('value'), which didn't give me an exception, but the JToken came back null.
I've tried a few other variations: $.outerObject.innerObject.property(?(@ == 'value')), $.outerObject.innerObject(?(@.property == 'value')), all of which still give nulls.
Can someone point out what I'm doing wrong, and if what I'm trying to do here is wrong as well, I'd appreciate being told early on before wasting too much more time on this.
SelectTokens(), filter expressions in aren't guaranteed to work for objects inside objects -- only objects inside arrays. See: JSONPath scripts not executing correctly for objects #1256: I'm not sure about this. Nothing in the JSONPath says that filters should apply to objects.. Sometimes workarounds can be found, see here or here.