say wanted to check if a path "L1.L2.L3" exists in a json object. There is a way to check the levels step by step (How to check whether json object has some property), but I wish to save the trouble, and check the path instead.
-
before serialization or after ? need to parse json string or serialized objectZ.R.T.– Z.R.T.2018-09-05 05:34:46 +00:00Commented Sep 5, 2018 at 5:34
-
possible duplicate stackoverflow.com/questions/7537398/…Gauravsa– Gauravsa2018-09-05 05:42:29 +00:00Commented Sep 5, 2018 at 5:42
-
check out these two: 1. newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm . 2. stackoverflow.com/questions/19892617/…NeverHopeless– NeverHopeless2018-09-05 05:49:10 +00:00Commented Sep 5, 2018 at 5:49
Add a comment
|
2 Answers
You can use SelectToken method from newtonsoft.json (token is null, when no match found):
string json = @"
{
""car"": {
""type"": {
""sedan"": {
""make"": ""honda"",
""model"": ""civics""
}
},
}
}";
JObject obj = JObject.Parse(json);
JToken token = obj.SelectToken("car.type.sedan.make",errorWhenNoMatch:false);
Console.WriteLine(token.Path + " -> " + token?.ToString());