Get path of JSON value using JSON.NET

I am trying to find a path of a JSON value. Consider the following JSON:

{
    "car": {
        "type": [{
            "sedan": {
                "make": "honda",
                "model": "civics"
            }
        },
        {
            "coupe": {
                "make": "ford",
                "model": "escort"
            }
        }]
    }
}

How can I get the path of the value "honda"? I'm looking to find something like this...

car_type_0_sedan_make_honda

Does JSON.NET support this? I see that there is a JToken.Path property but it is currently not available. http://json.codeplex.com/workitem/24136

Update to the latest version of Json.NET. The Path property was added to JToken in version 5.0 release 1 (April 7, 2013).

Here is a test program you can use to verify that it works:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""car"": {
                ""type"": [{
                    ""sedan"": {
                        ""make"": ""honda"",
                        ""model"": ""civics""
                    }
                },
                {
                    ""coupe"": {
                        ""make"": ""ford"",
                        ""model"": ""escort""
                    }
                }]
            }
        }";

        JObject obj = JObject.Parse(json);
        JToken token = obj["car"]["type"][0]["sedan"]["make"];
        Console.WriteLine(token.Path + " -> " + token.ToString());
    }
}

Output:

car.type[0].sedan.make -> honda

Querying JSON with complex JSON Path, This sample loads JSON and then queries values from it using SelectToken(​String) with a JSONPath query. Sample. Usage. Copy. JObject o = JObject.Parse(​  This sample loads JSON and then queries values from it using M:Newtonsoft.Json.Linq.JToken.SelectToken(System.String) with a JSONPathhttp://stackoverflow.com/tags

You could also try the SelectToken method like this:

var j = JObject.Parse(json);
var value = j.SelectToken("car.type[0].sedan.make");
Console.WriteLine(token.Path + " -> " + token.ToString());

Outputs:

car.type[0].sedan.make -> honda

Querying JSON with SelectToken, SelectToken; SelectToken with JSONPath; SelectToken with LINQ. SelectToken. SelectToken is a method on JToken and takes a string path to a child token. SelectToken returns the Find out more about JSONPath here. SelectToken With  The file above is actually pretty small and used for demo purposes. In practice, you can stumble upon terrifyingly big JSON files. NewtonSoft.Json or Json.NET is defacto the JSON standard for .NET, so it is being used to parse the JSON file. In order to deserialize this JSON to a C# object, you need a model class that represents the JSON nodes.

There is also a way to retrieve the path just by the value, using linq. You will need Json.NET for this.

JObject jo = JObject.Parse(json);
var token = jo.Descendants()
                    .OfType<JProperty>()
                    .Where(p => p.Value.ToString() == "honda")
                    .First();

        Console.WriteLine(token.Path);

See: https://dotnetfiddle.net/vZ1zLg

Querying JSON with JSON Path, This sample loads JSON and then queries values from it using SelectToken(​String). Sample. Usage. Copy. JObject o = JObject.Parse(@"{ 'Stores': [ 'Lambton​  JSONPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. Since a JSON structure is usually anonymous and doesn't necessarily have a "root member object" JSONPath assumes the abstract name $ assigned to the outer level object.

you can Convert the Json to dynamic object as below.

JavaScriptSerializer js=new JavaScriptSerializer();
var dataObject=Json.Decode(jsonString);

Then you can reflect over it and find out your string "honda" and construct the path as you like.

JToken.Path Property, Gets the path of the JSON token. Syntax. C#. Copy. public string Path { get; }. Property Value. Type: String. See Also. Reference. JToken Class · Newtonsoft. This is a step by step tutorial detailing: what JSON is, why you would use it, how to install JSON.Net and how to deserialize JSON strings using c# and json.net.

Querying JSON with JSON Path and LINQ, This sample loads JSON and then queries values from it using a combination of M:Newtonsoft.Json.Linq.JToken.SelectToken(System.String) and LINQ  Convert JSON to Value. Convert JSON to a Type. Casting JValue. Using JValue.Value. Using JObject.Properties. Using LINQ to JSON Annotations. Json.NET Home

JSON Path regex operator, This sample loads JSON and then queries values from it using M:Newtonsoft.​Json.Linq.JToken.SelectToken(System.String) with a regex JSON Path. Find Newtonsoft packages List<JToken> newtonsoftPackages = packages. Infers the type of primitive values in the JSON payload (other than null) and returns the stored string, long, double, boolean, or DateTime as a boxed object. Primitive values are single JSON values such as a JSON number, string, true, false, or null. Returns a JObject or JArray for complex values in the JSON payload.

Partial JSON deserialize by JsonPath with Json.NET – Automation , NET, so it is being used to parse the JSON file. In order to deserialize this JSON to a C# object, you need a model class that represents the  How to read AppSettings values from .json file in ASP.NET Core. Ask Question Asked 4 years, 9 months ago. Active 8 days ago. Viewed 300k times 236. 69. I have set up

Comments
  • I just installed the dll from NuGet but Visual Studio properties says Version 4.5.0.0. But looking in ~/packages/Newtonsoft.Json.5.0.8/Lib/net45, the properties on the file says version 5.0.8.16617. Anyway, I get this error [Method not found: 'System.String Newtonsoft.Json.LinqJToken.get_Path()'.]
  • It works for me. I added a test program to my answer that I used to test it. Is your project somehow referencing an older version of Json.Net? Do you have an old version installed in your GAC? If you run the above code in a new project, does that work? The version numbers you cited look correct-- the latest version has a DLL version of 5.0.8.16617 and an assembly version of 4.5.0.0.
  • I just tried this again on another computer and it works! I think you're right, my first computer must be referencing an old version somehow. I'll dig into that issue later but thank you so much!
  • I was looking for something like SelectToken because I thought an excellent library such as NewtonSoft.Json would have a nice feature like this :)
  • How would you search for value within the dataObject you have above?
  • var is not dynamic. Its type is inferred in compile-time.