I have a JSON data that I want to parse on my swift project, JSON data:
{
"receipt_id": 9498,
"status": "ACCEPTED",
"value": 100,
"promotionName": "Kampagne ",
"promotionId": 2062,
"imageUrl": "https://image.png",
"uploaded": "2022-02-22T11:58:21+0100"
}
On my project I have this code:
struct Receipt: Decodable {
let receiptId: Int?
let status: ReceiptStatus
let rejectionReason: String?
let value: Cent?
let promotionName: String
let promotionId: Int
let imageUrl: URL
let uploaded: Date
enum CodingKeys: String, CodingKey {
case receiptId = "receipt_id"
case status = "status"
case rejectionReason = "rejectionReason"
case value = "value"
case promotionName = "promotionName"
case promotionId = "promotionId"
case imageUrl = "imageUrl"
case uploaded = "uploaded"
}
}
When decoding JSON data this error appears:
'Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "imageUrl", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "receipts", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1)], debugDescription: "No value associated with key CodingKeys(stringValue: "imageUrl", intValue: nil) ("imageUrl"), converted to image_url.", underlyingError: nil))'
On decoding the JSON data I use convertFromSnakeCase but sometimes I don't want to follow this method for decoding so I force it inside codingKeys and that error appears