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
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).
- , ?