I have this JSON file.
[
{
"name": "January",
"holidays": [
{
"name": "New Year's Day",
"date": "2019-01-01T00:00:00-0500",
"type": {
"isNationalHoliday": true,
"isRegionalHoliday": true,
"isPublicHoliday": true,
"isGovernmentHoliday": true
}
},
{
"name": "Martin Luther King Day",
"date": "2019-01-21T00:00:00-0500",
"type": {
"isNationalHoliday": true,
"isRegionalHoliday": true,
"isPublicHoliday": true,
"isGovernmentHoliday": true
}
}
]
},
{
"name": "February",
"holidays": [
{
"name": "Presidents' Day",
"date": "2019-02-18T00:00:00-0500",
"type": {
"isNationalHoliday": false,
"isRegionalHoliday": true,
"isPublicHoliday": false,
"isGovernmentHoliday": false
}
}
]
},
{
"name": "March",
"holidays": null
}
]
I created a Month
struct to decode the dictionaries in the JSON.
public struct Month {
public let name: String
public let holidays: [Holiday]?
}
extension Month: Decodable { }
And an Year
struct to contain them all.
public struct Year {
public let months: [Month]
}
extension Year: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let values = try container.decode([Month].self)
months = values
}
}
My Holiday
struct is a little more complex due to having an enum called HolidayType
where I want to decode the values under the type
field in the JSON.
public struct Holiday {
public let name: String
public let date: Date
public let type: HolidayType
}
extension Holiday: Decodable { }
public enum HolidayType {
case isNationalHoliday
case isRegionalHoliday
case isPublicHoliday
case isGovernmentHoliday
enum CodingKeys: String, CodingKey {
case isNationalHoliday
case isRegionalHoliday
case isPublicHoliday
case isGovernmentHoliday
}
}
extension HolidayType: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self = try container.decode(HolidayType.self, forKey: .isNationalHoliday)
self = try container.decode(HolidayType.self, forKey: .isRegionalHoliday)
self = try container.decode(HolidayType.self, forKey: .isPublicHoliday)
self = try container.decode(HolidayType.self, forKey: .isGovernmentHoliday)
}
}
This is where I'm loading the file and decoding.
if let url = Bundle.main.url(forResource: "holidays", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let year = try decoder.decode(Year.self, from: data)
print(year.months)
} catch let error {
print("Error occurred decoding JSON: \(error)")
}
} else {
print("Error occurred loading file")
}
But it fails with the following error.
typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "holidays", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "type", intValue: nil), CodingKeys(stringValue: "isNationalHoliday", intValue: nil)], debugDescription: "Expected to decode Dictionary but found a number instead.", underlyingError: nil))
I can't figure out how to fix this. I also uploaded a demo project here.