Json.NET JSONPath request does not return expected results

I am using Newtonsoft Json.Net to select nodes from the following json:

{  
   "projects":[  
      {  
         "name":"Project 1",
         "client":{  
            "code":"ABC",
            "name":"Client 1"
         }
      },
      {  
         "name":"Project 2",
         "client":{  
            "code":"DEF",
            "name":"Client 2"
         }
      },
      {  
         "name":"Project 3",
         "client":{  
            "code":"GHI",
            "name":"Client 3"
         }
      }
   ]
}

Next c # snippet

//json is a JObject representation of the json listed above
var clients =  json.SelectTokens("$.projects[*].client");

Productivity:

[  
   {  
      "code":"ABC",
      "name":"Client 1"
   },
   {  
      "code":"DEF",
      "name":"Client 2"
   },
   {  
      "code":"GHI",
      "name":"Client 3"
   }
]

Which is cool, now what I would like to do is filter by client code, I would think that

$.projects[*].client[?(@.code == 'DEF')]

will work, but I clearly don't understand the syntax enough. This returns an empty list:

 var test1 =  json.SelectTokens("$.projects[*].client[?(@.code == 'DEF')]").ToList();

And the only marker selector returns null:

  var test2 =  json.SelectToken("$.projects[*].client[?(@.code == 'DEF')]");

I tried several different configurations https://jsonpath.curiousconcept.com/ and it seems that my query syntax is really broken.

Using the JSONPath 0.3.4 streaming communication implementation (at the link above) I can get the client using

$..[?(@.code == 'DEF')]

however, this syntax does not seem to be correct for Json.Net (and not for Goessner implementation on the same page).

- , ?

+4
1

Json.NET JSONPath parser [?( )] (script) . ,

var test = json.SelectTokens(@"$.projects[?(@.client.code == 'DEF')].client");

, ,

[
  {
    "code": "DEF",
    "name": "Client 2"
  }
]

. .

, , JSONPath jsonpath.curiousconcept.com , jsonpath.com, https://github.com/ashphy/jsonpath-online-evaluator. JSONPath , () script, script , "" .

, -, Json.NET, 10.0.3 - . № 1256: JSONPath . ( , , .) , , ( ).

+3

Source: https://habr.com/ru/post/1682191/


All Articles