Convert JSON to Swift Codable struct!

Input JSON to generate Swift Codable struct code (with CodingKeys), including nested objects! Ready for iOS JSONDecoder.
JSON
Result
 

Convert JSON to Swift Codable struct!

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.

How to use

  1. Paste a response body from your API, for example one captured with Proxyman or Charles.
  2. Set Type name to the top-level struct name (for example EpisodePage).
  3. Paste the result into a Swift file; import Foundation is included.
  4. Decode with try JSONDecoder().decode(EpisodePage.self, from: data).

Example

Input (JSON)
{
  "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
}
Output (Swift · Codable)
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.

Supported features

  • Every struct adopts Codable; properties are immutable lets.
  • snake_case and kebab-case keys become camelCase properties, and only then is enum CodingKeys: String, CodingKey added with the original keys.
  • Types: String, Int, Double, Bool, and [T] for arrays.
  • Fields missing from some items or containing null are Optionals (String?), so missing keys still decode.
  • Values of mixed type use a generated, decodable JSONValue enum.
  • Swift keywords such as default, class or self are wrapped in backticks.
  • A top-level array produces typealias Root = [RootItem].

Use cases

URLSession with async/await

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).

SwiftUI list screens

Add Identifiable to the generated struct (when it has an id field) and pass it straight to List(page.episodes) { ... }.

Bundled mock data

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.

Notes and limitations

  • Date strings come out as String. To get Date, change the property type and set a strategy such as decoder.dateDecodingStrategy = .iso8601.
  • If a non-optional property's key is missing from a response, decoding throws DecodingError.keyNotFound. Add ? to fields the API may omit, even if your sample always had them.
  • All properties are let; switch to var for models you mutate as view state.
  • No extra protocols such as Identifiable or Hashable are adopted.
  • The JSONValue enum is helper code for mixed types; if your project already has an equivalent type, delete it and use yours.

FAQ

With 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.

Why structs and not classes?

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.

A field I expected to be an integer came out as 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.

Does encoding back to JSON keep the original keys?

CodingKeys are used for encoding too, so JSONEncoder writes guest_name again. Optional properties that are nil are omitted from the output.