3

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.

anonmous
  • 139
  • 1
  • 6
  • 2
    I haven't tested your case, but in general filtering using `SelectToken()` isn't guaranteed to work for objects inside objects -- only objects inside arrays. See: [JSONPath scripts not executing correctly for objects #1256](https://github.com/JamesNK/Newtonsoft.Json/issues/1256#issuecomment-289134144): *I'm not sure about this. Nothing in the JSONPath says that filters should apply to objects.*. Sometimes workarounds can be found, see [here](https://stackoverflow.com/a/45298348/3744182) or [here](https://stackoverflow.com/a/39453636/3744182). – dbc Apr 02 '21 at 18:41

0 Answers0