Convert JSON into Swift structs that adopt Codable. Properties are declared with let and renamed to camelCase, and a CodingKeys enum is generated only for structs where a name differs from its JSON key.
Use it when an iOS or macOS app needs a model to decode a URLSession response with JSONDecoder, or when you want types matching the mock JSON behind a SwiftUI preview.
Type name to the top-level struct name (for example EpisodePage).import Foundation is included.try JSONDecoder().decode(EpisodePage.self, from: data).{
"podcast": "Swift Talk",
"episodes": [
{ "id": 301, "title": "Async Sequences", "duration": 1840, "explicit": false },
{ "id": 302, "title": "Macros", "duration": 2215.5, "explicit": false, "guest_name": "Chris" }
],
"total_count": 2
}import Foundation
struct EpisodePage: Codable {
let podcast: String
let episodes: [Episode]
let totalCount: Int
enum CodingKeys: String, CodingKey {
case podcast
case episodes
case totalCount = "total_count"
}
}
struct Episode: Codable {
let id: Int
let title: String
let duration: Double
let explicit: Bool
let guestName: String?
enum CodingKeys: String, CodingKey {
case id
case title
case duration
case explicit
case guestName = "guest_name"
}
}
Elements of the episodes array become an Episode struct. duration mixes an integer (1840) and a decimal (2215.5), so it is Double; guest_name, found only in the second episode, is guestName: String?. Any struct with a renamed field gets a CodingKeys enum listing all of its keys.
Codable; properties are immutable lets.enum CodingKeys: String, CodingKey added with the original keys.String, Int, Double, Bool, and [T] for arrays.null are Optionals (String?), so missing keys still decode.JSONValue enum.default, class or self are wrapped in backticks.typealias Root = [RootItem].From a single sample response you get a model you can decode on the line right after let (data, _) = try await URLSession.shared.data(from: url).
Add Identifiable to the generated struct (when it has an id field) and pass it straight to List(page.episodes) { ... }.
Ship the sample JSON from the design phase in the app bundle and decode it with the same model, so screens can be built before the real API is ready.
String. To get Date, change the property type and set a strategy such as decoder.dateDecodingStrategy = .iso8601.DecodingError.keyNotFound. Add ? to fields the API may omit, even if your sample always had them.let; switch to var for models you mutate as view state.Identifiable or Hashable are adopted.JSONValue enum is helper code for mixed types; if your project already has an equivalent type, delete it and use yours.keyDecodingStrategy = .convertFromSnakeCase, can I delete the CodingKeys?Yes, the decoder then converts guest_name ↔ guestName for you. Don't combine the two, though: the decoder converts keys to camelCase first and then compares them against the "guest_name" raw value in CodingKeys, so matching fails.
Treating response models as value types is the usual Swift convention, and structs get a memberwise initializer for free, which makes test data easy to build. If you need reference semantics, change to final class and add an initializer.
Double.When a key holds both integers and decimals, Double avoids losing precision. Conversely, if every sample value looks like 4.0, the browser's JSON parser reads it as an integer and you may get Int, so check real API values.
CodingKeys are used for encoding too, so JSONEncoder writes guest_name again. Optional properties that are nil are omitted from the output.