Decodable is used to convert JSON data into Swift models.
It allows structured data from an API to be mapped directly into Swift types.
JSON → Decodable Model → Swift Object
Instead of manually parsing JSON, Combine uses JSONDecoder to handle conversion automatically.
struct EmployeeResponse: Decodable {
let employeeId, depid: Int
let salary: Double
let name, role, joining, workPhone: String
}
The model structure must match the JSON structure.
When JSON keys do not match Swift property names:
enum CodingKeys: String, CodingKey {
case depid = "dep_id"
case joining = "joining_date"
case employeeId = "id"
}
Used for:
snake_case → camelCase conversion
custom key mapping
Used to decode raw data: