Newtonsoft JSON.Net SelectToken Issue

I have the following request and a sample JSON. I try it at http://jsonpath.com/ "it works as expected. If I try it in VisualStudio, it will not get any results.

$.Items.Services[?(@.Name ==  'Another Service')].Url

Here's the JSON:

{
"Items": {
    "Resource": {
        "Id": "12345"
    },
    "Services": {
        "service1": {
            "Name": "My First Service",
            "Type": "WS",
            "Url": "https://server1/service1"
        },
        "service2": {
            "Name": "Another Service",
            "Type": "WS",
            "Url": "https://server2/service2"
        }
    }
}   
}

And an example code:

JObject obj = JObject.Parse(File.ReadAllText(@"d:\temp\sample.json"));
var matches = obj.SelectTokens("$.Items.Services[?(@.Name ==  'Another Service')].Url");
if(matches != null)
{
    foreach(var item in matches)
    {
       item.Replace(replacement); // this never gets executed
    }
 }
+2
source share
1 answer

Try the following:

var matches = obj.SelectTokens("$.Items.Services[?(@..Name == 'Another Service')]..Url");
+1
source

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


All Articles