0

I feel like this question must have been asked before, but maybe I don't know the terminology for what I'm describing here.

I'm stuck using a web service that returns JSON in which things you would normally expect to be arrays are instead children with names you cannot know ahead of time. For example, instead of this:

{
  "orders": [
    { ... },
    { ... },
    { ... }
  ]
}

It gives me this:

{
  "orders": {
    "order-42": { ... },
    "order-51": { ... },
    "order-102": { ... }
  }
}

I could parse the document as a JToken and grab the children of "orders", but then I have the same problem again with the children of the order. For example, instead of the order property "lines" being an array, it has children named "1", "2", etc.

To make matters worse, in some cases the names of the children contain information I need that seems like it should have been a property of the children.

What's the simplest way to deal with this?

StackOverthrow
  • 1,158
  • 11
  • 23
  • You can deserialize `orders` into a `Dictionary` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182). I.e. your root object would be `public class RootObject { public Dictionary orders { get; set; } }` In fact this looks to be a duplicate, agree? – dbc Jan 21 '21 at 23:33
  • @dbc Thanks. Not sure yet, but I think my question could be a duplicate of your second link. – StackOverthrow Jan 21 '21 at 23:36
  • There is also [Deserializing JSON when key values are unknown](https://stackoverflow.com/a/24901245/3744182) or [Deserializing JSON with unknown object names](https://stackoverflow.com/q/38688570/3744182) whose answers also propose using a dictionary. – dbc Jan 21 '21 at 23:37

0 Answers0