Learn how to serialize and deserialize JSON data using Swift's Codable protocol.

struct Person: Codable {
    var name: String
    var age: Int
}

let json = """
{"name": "Alice", "age": 30}
"""

if let data = json.data(using: .utf8) {
    if let person = try? JSONDecoder().decode(Person.self, from: data) {
        print("Name: \(person.name), Age: \(person.age)")
    }
}
Share this post