Swift Decodable JSON dictionary with heterogeneous array

Viewed 3122

I have some JSON that comes back in the following format,

{
"Random Word": [
    [
        "2017-08-10",
        6
    ],
    [
        "2017-08-11",
        6
    ],
    [
        "2017-08-15",
        4
    ]
],
"Another Random Word": [
    [
        "2017-08-10",
        4
    ],
    [
        "2017-08-11",
        4
    ],
    [
        "2017-08-12",
        1
    ],
    [
        "2017-08-14",
        2
    ],
    [
        "2017-08-15",
        4
    ],
    [
        "2017-08-16",
        1
    ]
]
}

The issue is that the 'key' will be different each time and the 'value' contains a heterogeneous array of Strings (that should be converted to Dates), and Ints.

Is there a way to use Swift's Decodable protocol to turn this into objects?

Here is a Struct that it could be decoded as,

struct MyJSONData: Decodable {

    var myInfo: Dictionary<String, [[Any]]>?
    ...
}

However, if there is a better way to structure the struct, Im all ears!

Thanks in advance.

2 Answers
Related