Decoding JSON with Codable
Map JSON into Swift model structs.
Decoding JSON with Codable is a free SwiftUI Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why JSON Needs Decoding
APIs speak JSON, a text format Swift cannot use directly. You decode that text into real Swift types you can work with. 📦
Meet Codable
The Codable protocol lets a type convert itself to and from JSON automatically, so you rarely write parsing code by hand.
A Codable Struct
Just add Codable to your struct. Swift generates the encoding and decoding for every stored property for you.
struct User: Codable {
let id: Int
let name: String
}Matching JSON Keys
Swift maps each property name straight to the matching JSON key. So "name" in JSON fills the name property exactly.
{ "id": 1, "name": "Ada" }Decoding with JSONDecoder
A JSONDecoder turns raw Data into your type. You tell it which type to expect, and it does the rest.
let user = try JSONDecoder()
.decode(User.self, from: data)Working With Data
Network responses arrive as Data, a bag of bytes. The decoder reads those bytes and produces your typed value.
Decoding Arrays
To decode a JSON list, ask for an array of your type. One call gives you a ready-to-use Swift array. 🎉
let users = try JSONDecoder()
.decode([User].self, from: data)Renaming with CodingKeys
When JSON keys differ from your names, add CodingKeys to map a snake_case key onto a clean Swift property.
enum CodingKeys: String, CodingKey {
case name = "full_name"
}Optional Properties
Mark a property optional when the key may be missing. The decoder simply sets it to nil instead of throwing.
let avatar: String?Decoding Can Throw
If the JSON shape does not match your type, decoding throws. Always wrap the call in try so you can handle bad data.
Nested Models
Nested JSON objects become nested structs. Make each level Codable and Swift decodes the whole tree at once.
struct Post: Codable {
let author: User
}Quick Check
Quick check on turning JSON into Swift types.
Recap: Codable
You learned that Codable plus JSONDecoder maps JSON into typed Swift values, with CodingKeys for renames and optionals for missing keys. 🙌
Frequently asked questions
Is the “Decoding JSON with Codable” lesson free?
Yes — the full text of “Decoding JSON with Codable” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Decoding JSON with Codable”?
Map JSON into Swift model structs. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Decoding JSON with Codable” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Decoding JSON with Codable
- Calling APIs with URLSession
- The .task Modifier
- Loading & Error States