NewtonSoft JsonPath with C# Syntax

Viewed 230

I have a set of objects which I'm deserializing using NewtonSoft.Json 13.0.1. My project is using .Net 5.0 and the language is C#. I want to use JsonPath (Json Path) syntax to determine whether an object matches a given criterion, in particular, whether parent.child.property1 matches the string 'appname.' It seems that the only way I can use a filter expression is if I have an array. e.g., in the following, I have been unable to come up with a JsonPath expression which works. An example Json is as follows:

{
"parent": {
    "child": {
        "property1": "appname",
        "property2": "57fc697c44",
        "property3": "yes"
    }
}}

As a workaround, what I've done is wrap the previous Json in an array, and then used a filter expression, so, with the following Json:

{
"item": [
    {
        "parent": {
            "child": {
                "property1": "appname",
                "property2": "57fc697c44",
                "property3": "yes"
            }
        }
    }
]}

and this JsonPath expression:

$..[?(@.parent.child.property1=='appname')]

I get a match.

As an aside, I originally tried (notice the single dot)

$.[?(@.parent.child.property1=='appname')]

which didn't work. I don't understand why because my understanding is that '$.' refers to the root of the Json. If anyone can help me to understand this as well, I'd appreciate it.

Thank you.

0 Answers
Related