Concept

Decodable is used to convert JSON data into Swift models.

It allows structured data from an API to be mapped directly into Swift types.


Key Idea

JSON → Decodable Model → Swift Object

Instead of manually parsing JSON, Combine uses JSONDecoder to handle conversion automatically.


Defining a Model

struct EmployeeResponse: Decodable {
    let employeeId, depid: Int
    let salary: Double
    let name, role, joining, workPhone: String
}

The model structure must match the JSON structure.


Mapping JSON Keys

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

JSONDecoder

Used to decode raw data: