DecodingError 🍎 iOS

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

  1. Print the full error to see codingPath
  2. Make fields optional or add CodingKeys
  3. Use keyDecodingStrategy = .convertFromSnakeCase; check the raw response

Detailed fix by platform

Swift

  1. 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

  1. Path — codingPath in the error
  2. Raw — Actual JSON body
  3. 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:

🧠 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.