I have a Json file as below
{
"objectId": "123",
"properties": {
"objectId": "456"
},
"variables": [
{
"objectId": "789"
},
{
"objectId": "012"
}
]
}
I want to get all 'objectId's in a list like [ "123", "456", "789", "012" ]
I tried as below
var body = JObject.Parse(jsonString); //using Newtonsoft library
var list = body.Properties().Where(p => p.Name == "objectId").Select(p => p.Value).ToList();
I tried in a below way too
var list = new List<string>();
foreach(var prop in body.Properties())
{
if(prop.Name == "objectId")
{
list.Add(prop.Value.ToString());
}
}
But here i get only first level properties.
Use the DescendantsAndSelf().OfType() to get all JProperties, and then filter with LINQ.
var root = (JContainer)JToken.Parse(json);
var list = root.DescendantsAndSelf().OfType<JProperty>().Where(p => p.Name == "objectId").Select(p => p.Value.Value<string>());
Console.WriteLine(string.Join(",", list.ToArray()));
IEnumerable<JToken> AllTokens(JObject obj)
{
var toSearch = new Stack<JToken>(obj.Children());
while (toSearch.Count > 0)
{
var inspected = toSearch.Pop();
yield return inspected;
foreach (var child in inspected)
{
toSearch.Push(child);
}
}
}
Then you can use linq to filter and perform action:
var tokens = AllTokens(body);
var data = tokens.Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "objectId")
.Select(x=> ((JProperty)x).Value.Value<string>()).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With