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
3
Answers
Try this:
It’wrong is a
let imageUrl: URL, let uploaded: Date, let status: ReceiptStatus
Because the value of
imageUrl
,uploaded
,status
isString
,If you using
Date
,URL
andcustom enum type
of your model. Please usingto cast new type you want or you set
Optional
toimageUrl
anduploaded
,status
.Apple Document
Seems like the issue has nothing to do with the
keyDecodingStrategy
(convertFromSnakeCase). I think that theimageUrl
is not a required field in your JSON and can be equal to null or even miss sometimes. Making theimageUrl
property in your struct optional will fix the problem.