Json.net: Can JObject.SelectToken do the same thing XPath can do? If so, what is the syntax?

The response to the query , published in 2009, was to use the new JObject.SelectToken function, which should deliver XPath functionality. Now I am using JSON.NET 4.5 R11 and SelectToken .

But I could not find much documentation (mostly syntax) regarding the path string that should be passed to the SelectToken function.

The following code creates a Json string and on which I would like to execute the Xpath method (i.e. as far as I know SelectToken )

 IList branches = new ArrayList(); IList employees = new ArrayList(); employees.Add(new { EmpId = 1, Name = "Name1" }); employees.Add(new { EmpId = 2, Name = "Name2" }); employees.Add(new { EmpId = 3, Name = "Name3" }); IList employees2 = new ArrayList(); employees2.Add(new { EmpId = 4, Name = "Name1" }); employees2.Add(new { EmpId = 5, Name = "Name5" }); branches.Add(new { BranchName = "Branch1", Employees = employees }); branches.Add(new { BranchName = "Branch2", Employees = employees }); string json = JsonConvert.SerializeObject(branches); var branchesDeserialised = JsonConvert.DeserializeAnonymousType(json, new[] { new { BranchName = "", Employees = new[] { new { EmpId = 0, Name = "" } } } }); JArray ja = JArray.Parse(json); var AllName1Tokens = ja.SelectToken(@"..Name=""Name1"""); //Get all names that are having Name = "Name1" irrespective of branch 

Since I don't have class binaries, and the Json string structure is so great that it will be difficult to use dynamics. Therefore, using LINQ for objects after de-serialization is not possible. I do not want to convert the Json string to XML or any other format to make a choice. Also, I do not want to write code to analyze it.

What is the syntax of the Path parameter of the SelectToken function? How to select all EmpId from employees , where Name="Name1" ?

Edit1 : is it possible to get the result using JObject.Select (LINQ query) in the JSON string (and not on the real object) if SelectToken not able to do this? How about regex?

+6
source share
2 answers

From the author of JSON.NET:

Since Json.NET 6.0 selects SelectToken with full JSONPath support, XPath is a query language for JSON.

 JObject o = JObject.Parse(@"{ ""Manufacturers"": [ { ""Name"": ""Acme Co"", ""Products"": [ { ""Name"": ""Anvil"", ""Price"": 50 } ] }, { ""Name"": ""Contoso"", ""Products"": [ { ""Name"": ""Elbow Grease"", ""Price"": 99.95 }, { ""Name"": ""Headlight Fluid"", ""Price"": 4 } ] } ] }"); // manufacturer with the name 'Acme Co' var acme = o.SelectToken("$.Manufacturers[?(@.Name == 'Acme Co')]"); 

Blog Post Details

+3
source

Can JObject.SelectToken do the same thing XPath can do?

What is the syntax of the Path parameter for the SelectToken function?

I believe that it only supports the string path to a single token, for example, "branches[0].employees[0].name"

How to select all EmpId employees where Name = "Name1"?

I'm not sure if SelectToken can do this, and the limitations of your question preclude the most common solutions.

I could not find much documentation (mainly syntax) regarding the path string that should be passed to the SelectToken function.

Some documentation here :

The path consists of property names and array indices, separated by periods. Array indices can use square or parentheses.

+3
source

All Articles