Search code examples
swiftswift5urlsessionjsondecoder

Decoding raw data from URLSession the right way in Swift


The response for most of the endpoints in my API is something like this -

{
   "status":"success",
   "data":[
      {
         "id":"1",
         "employee_name":"Tiger Nixon",
         "employee_salary":"320800",
         "employee_age":"61",
         "profile_image":""
      },
      {
         "id":"2",
         "employee_name":"Garrett Winters",
         "employee_salary":"170750",
         "employee_age":"63",
         "profile_image":""
      }
   ]
}

And this is what my Employee model looks like

struct Employee: Codable {
    let id, employeeName, employeeSalary, employeeAge: String
    let profileImage: String?

    enum CodingKeys: String, CodingKey {
        case id
        case employeeName = "employee_name"
        case employeeSalary = "employee_salary"
        case employeeAge = "employee_age"
        case profileImage = "profile_image"
    }
}

typealias Employees = [Employee]

I just want to extract the data part of the API response using JSONDecoder and pass it to my completion handler

completionHandler(try? JSONDecoder().decode(Employees.self, from: data), response, nil)

I was able to get around by creating a struct Employees like this -

struct Employees: Codable {
    let status: String
    let data: [Employee]
}

But this is just a workaround and I would have to do it for every almost every model. So is there a better and less redundant way of extracting data from the response?


Solution

  • What I would do if I were you is just create a template-based wrapper and use it for all of your model objects.

    struct ModelWrapper<T: Codable>: Codable {
        let status: String
        let data: [T]
    }
    let decoder = JSONDecoder()
    let wrapper = try decoder.decode(ModelWrapper<Employee>.self, from: json)