2

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.

1
  • I haven't investigated your example yet, but with 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. Commented Dec 9, 2019 at 23:48

1 Answer 1

0

You could validate property == value using

"$.outerObject[?(@..property == 'value')]"

If you want to fetch the value, you could use

"$.outerObject[?(@..property == 'value')]..property"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.