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
}
}
Solution 1:
Try this:
var matches = obj.SelectTokens("$.Items.Services[?(@..Name == 'Another Service')]..Url");