BeCuriouzLearn something new every day
How one word makes Swift read JSON for you
💡 Codable makes the Swift compiler generate your JSON encode/decode code at build time — but only if every property is also Codable.
You almost never write JSON parsing by hand in Swift anymore. Mark a type as Codable and the compiler quietly writes the serialization code for you at build time. It's not runtime reflection or magic strings — it's real code the compiler synthesizes from your property names and types.
Codable is actually just a convenience: typealias Codable = Decodable & Encodable. Decodable means "can be built from an external format like JSON," and Encodable means "can be turned back into it." The catch: synthesis only works if every stored property is itself Codable. Add one property whose type isn't (say a custom class that never conformed), and the compiler stops synthesizing and demands you write the methods yourself.
When your Swift property names don't match the JSON keys — JSON loves snake_case, Swift prefers camelCase — you don't abandon synthesis. You add a nested enum CodingKeys: String, CodingKey mapping each property to its JSON name, and the compiler still generates everything else. Decoding is then one line: try JSONDecoder().decode(User.self, from: data).
The key mental shift: Codable is a compile-time contract, not a runtime parser. If it compiles, the encode/decode logic already exists — mismatches surface as build errors or typed DecodingErrors, not silent nils the way older NSDictionary-based parsing did.