I have a json-object in C# (represented as a Newtonsoft.Json.Linq.JObject object) and I need to flatten it to a dictionary. Let me show you an example of what I mean:
<
JObject jsonObject=JObject.Parse(theJsonString);
IEnumerable jTokens = jsonObject.Descendants().Where(p => p.Count() == 0);
Dictionary results = jTokens.Aggregate(new Dictionary(), (properties, jToken) =>
{
properties.Add(jToken.Path, jToken.ToString());
return properties;
});
I had the same requirement of flattening a nested json structure to a dictionary object. Found the solution here.