Swift DecodingError: keyNotFound / typeMismatch / valueNotFound / dataCorrupted
JSONDecoder couldn’t map the JSON to your Codable type — a key is missing, a value has a different type or is null, or the data isn’t valid JSON.
Seen on:
iOS
Meaning
The error’s codingPath tells you exactly which field failed. APIs returning snake_case, numbers as strings, nulls, or an HTML error page (dataCorrupted) are typical.
Common causes
- Field missing in JSON (keyNotFound)
- Different type (string vs number) (typeMismatch)
- null for a non-optional property (valueNotFound)
- Response isn’t JSON (error page) (dataCorrupted)
- snake_case keys without keyDecodingStrategy
⚡ Quick fix
- Print the full error to see codingPath
- Make fields optional or add CodingKeys
- Use keyDecodingStrategy = .convertFromSnakeCase; check the raw response
Detailed fix by platform
Swift
- swift
let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase do { _ = try decoder.decode(User.self, from: data) } catch { print(error) ; print(String(data: data, encoding: .utf8) ?? "") }
How to diagnose
- Path — codingPath in the error
- Raw — Actual JSON body
- Types — Model vs JSON
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Could not cast value of type Swift: Could not cast value of type 'X' to 'Y'
- entity.parse.failed API Error: Malformed JSON in request body (entity.parse.failed / JSON parse error)
- is an invalid start of a value System.Text.Json: '<' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0
Most viewed in iOS
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026