Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft JSON.Net SelectToken Issue

I have the following query and the sample JSON. I try it on "http://jsonpath.com/" it works as expected. If I try it in VisualStudio it returns no 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 the sample 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
    }
 }
like image 443
Jeff Saremi Avatar asked Oct 19 '25 10:10

Jeff Saremi


1 Answers

Try this:

var matches = obj.SelectTokens("$.Items.Services[?(@..Name == 'Another Service')]..Url");
like image 193
Jim Hewitt Avatar answered Oct 22 '25 03:10

Jim Hewitt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!