JSON decode unknown number of fields

There are a couple different ways to model this data structure. When the keys of an object are dynamic, often you’ll want to model this in Elm with a Dict. However, it looks like they’re really just incrementing with each value so many these are better handled as a List. That’s up to you!

If you go down the Dict path, perhaps your elm model might look like { boxes : Dict String (Dict String String) } (You could also decide to convert the string keys to integers if you wanted). To go down this route, you would use the dict decoder. The dict decoder takes in a decoder for the value and then decodes the keys as strings. The inner items decoder would look something like this: Json.Decode.dict Json.Decode.string and would have the signature: Decoder (Dict String String). You would nest this decoder inside another Dict decoder to make your final decoder.

If you go down the List path, your model could look something like { boxes : List (List String) }. To Decode that, you have to do a couple steps. You could use the key-value pairs decoder to decode the object into a list of the keys and values. You could take that result and transform it into just your items by only taking the values. That decoder works like the dict decoder in that it takes in a decoder for the values and keeps the keys a String. You can decode the items part like this: Json.Decode.keyValuePairs (Json.Decode.String) |> Json.Decode.map (List.map (\(key, value) -> value)). That would decode your object of items into a list of the contained items. This would have the signature Decoder (List String).

I hope that answers your question and points you in the right direction!