I have JSON data, where the structure looks like this:
{
"name": "Abaddon",
"inherits": "Curse",
"item": "Megaton Raid Belt",
"itemr": "God's Hand Belt",
"level": 75,
"arcana": "Judgement",
"elems": {
"physical": "ab",
"gun": "ab",
"fire": "-",
"ice": "-",
"electric": "-",
"wind": "-",
"psychic": "-",
"nuclear": "-",
"bless": "rs",
"curse": "ab"
},
"skills": {
"Mabufudyne": 0,
"Megaton Raid": 0,
"Enduring Soul": 0,
"Flash Bomb": 78,
"Ailment Boost": 79,
"Absorb Phys": 80,
"Gigantomachia": 81
},
"stats": {
"strength": 51,
"magic": 42,
"endurance": 58,
"agility": 38,
"luck": 43
},
"trait": "Mouth of Savoring",
"area": "Da'at",
"floor": "All"
}
I'm trying to decode it into this model:
struct Persona: Codable, Identifiable {
var id = UUID()
let name: String
let special: Bool?
let inherits: String?
let item: String
let itemR: String
let skillCard: Bool?
let arcana: ArcanaType
let level: Int
let stats: Stats
let elems: ElementReactions
let skills: SkillsCollection?
let personality: PersonalityType?
let mementosArea: [AreaType]?
let floor: String?
let trait: String?
let rare: Bool?
let dlc: Bool?
enum CodingKeys: String, CodingKey {
case name, special, inherits, item, itemR = "itemr", skillCard, arcana, level, stats, elems, skills, personality, mementosArea, floor, trait, rare, dlc
}
}
However, I'm getting this error:
Key 'CodingKeys(stringValue: "itemr", intValue: nil)' not found: No value associated with key CodingKeys(stringValue: "itemr", intValue: nil) ("itemr").
I'm sure I'm missing something simple, but I can't figure out why itemr isn't decoding, and where the "intValue: nil" is coming from in the error. All the other fields decode perfectly.
Here's the code I'm using to decode:
extension Bundle {
func decode<T: Decodable>(_ type: T.Type, from file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file)")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file)")
}
let decoder = JSONDecoder()
do {
let loaded = try decoder.decode(T.self, from: data)
return loaded
} catch DecodingError.dataCorrupted(let context) {
print(context)
} catch DecodingError.keyNotFound(let key, let context) {
print("Key '\(key)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch DecodingError.valueNotFound(let value, let context) {
print("Value '\(value)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch DecodingError.typeMismatch(let type, let context) {
print("Type '\(type)' mismatch:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch {
print("error: ", error)
}
fatalError("Failed to decode \(file)")
}
}