Currently, many applications receive information from the servers with which they connect in JSON format.
With Swift 4, this process has been simplified thanks to the Encodable and Decodable protocols. The struct or class that adopt these protocols may be encoded to a JSON format or decoded from that format, with a few lines of code.

  • Encodable protocol. Convert instances of a specific type to format, for example, JSON.
  • Decodable protocol Converts information in format, for example, JSON to instances of a certain type.
  • Codable protocol. It is a typealias definition for a protocol that adopts Encodable and Decodable protocols.
typealias Codable = Encodable & Decodable

A struct or class may adopt the Codable type if its properties are also of the Codable type:

  • String, Int, Double, Data, URL
  • Array, Dictionary, Optional (if contain Codable types)


Let’s see now how to work with the Decodable protocol. We will assume that our application calls a web service that returns the information of a specific user in JSON format:

let userJson = """
  "id": "39631383-4e2a-48ef-bb40-f9d896392eab",
  "firstName": "Jane",
  "lastName": "Doe",
  "mail": "",
  "active": "true"

To pass this information to an instance of our struct User:

struct User: Codable { // User conforms to Decodable protocol
  let id: String
  let firstName: String
  let lastName: String
  let mail: String
  let active: String

let jsonData = .utf8)! // Json is converted to Data type
do {
    let user = trey JSONDecoder().decode(User.self, from: jsonData) // Data is decoded
} catch {


If we want to obtain a JSON from an instance of our struct User:

var user = User() = "39631383-4e2a-48ef-bb40-f9d896392eab"
user.firstName = "Jane"
user.lastName = "Doe"
user.mail = "" = "true"

do {
    let jsonData = try JSONEncoder().encode(user)
    print(String(decoding: jsonData, as: UTF8.self))
} catch {
Categories: CodableSwift

1 Comment · 11 June 2020 at 16:09
Very good article! We will be linking to this great article on our
site. Keep up the good writing.

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow on Feedly