It looks like good JSON objects typically have a name-value pair, with value being JSON array if it has multiple items. That’s easier to parse.
But I come across an annoying case, I’m unsure how to parse. While my question is specific to .NET and I’m using JSON.NET library on CodePlex (primarily LINQ to JSON approach, but could take other approaches), also wonder how this is handled in other language platforms as well.
The annoying case is JSON without arrays and name-value pairs only at the lowest level children, JSON object whose nodes contain more objects until the lowest level where we get properties as name-value pairs. And I want to extract a list of the intermediate nodes and I don’t know their node names beforehand while doing query (they’re dynamic). How do you query for them or iterate through them. Here’s an example below – I want all the names of the nodes that are children of “response”. The parent name of the albumData properties that is, as in “Twenty5MBPhotos” and “TestPhotos”.
{"status":200,"message":"OK","response":{"Twenty5MBPhotos":{"__albumData":{"id":"67b0de21d48290741598"}},"FiveMBPhotos":{"__albumData":{"id":"67b0de21d4a72ac5b401"}},"TestPhotos":{"__albumData":{"id":"67b0de21d64693a7f49f"}},"Sample album":{"__albumData":{"id":"67b0de21d67c4a99f4c3"}},"Empty":{"__albumData":{"id":"67b0de21d5fc3f2e05ac"}},"me":{"__albumData":{"id":"67b0de21d5bd2d1ea4f9"}},"satx":{"__albumData":{"id":"67b0de21d5b2f929c473"}},"designs":{"__albumData":{"id":"67b0de21d5a29b9f45c0"}},"views":{"__albumData":{"id":"67b0de21d5958c3b652e"}},"Puppy":{"__albumData":{"id":"67b0de21d540884dc413"}},"QaTest":{"__albumData":{"id":"67b0de21d6d96c35d419"}},"random":{"__albumData":{"id":"67b0de21d6b89b9ab401"}}}}
frankly, the JSON format could have been easier with result being an array with properties album name, and album ID instead of current format, but I didn’t define this.
You can get all the names like this:
Just need to cast the JToken as a JProperty to get the name value.
Update:
Or with some linq