JSON to Swift Codable

Convert JSON to Swift structs or classes conforming to Codable with optional CodingKeys.

Options

Type
Access Level
struct Root: Codable {
    let userId: Int
    let firstName: String
    let email: String
    let isActive: Bool
    let score: Double
    let tags: [String]
    let address: Address
    let createdAt: String
}

struct Address: Codable {
    let street: String
    let city: String
    let zipCode: String
}

Swift Codable: the type-safe way to work with JSON in iOS and macOS apps

Before Swift 4 introduced Codable, iOS developers relied on JSONSerialization with dictionaries of [String: Any]: a verbose, error-prone approach with no compile-time safety. Codable changed that by letting the compiler generate the decoding and encoding logic automatically from your type definitions. The result is that a single conformance declaration (": Codable") is usually all you need to parse a REST API response into a strongly-typed Swift model.

Writing Codable structs by hand from a JSON API response is tedious and error-prone: a mistyped property name or wrong type causes silent decoding failures. This generator reads the JSON structure and produces accurate Swift types, handling nested objects, optional values, and arrays automatically. Paste the JSON, name your root type, and you have a working model in seconds.

How to convert JSON to Swift: step by step

  1. 1
    Paste your JSON
    Copy any valid JSON object or array and paste it into the left input area. Complex nested objects and arrays generate nested Swift types automatically.
  2. 2
    Set the root type name
    Type a name for the top-level Swift type. Use PascalCase as per Swift conventions: for example UserResponse or Product. Nested types are named from their JSON key.
  3. 3
    Choose struct or class
    Select struct (recommended for pure data models: value semantics, automatically Equatable) or class (reference semantics, useful if you need inheritance or mutable shared state).
  4. 4
    Choose your conformance
    Select Codable to generate both Decodable and Encodable conformance, Decodable-only for response parsing, or Encodable-only for request encoding.
  5. 5
    Copy and paste into Xcode
    Click Copy to grab the generated Swift code and paste it into a new .swift file in your Xcode project. The types are ready to use with JSONDecoder immediately.

Related Tools

Frequently Asked Questions

What is Codable in Swift?
Codable is a type alias for the Decodable & Encodable protocol combination. Types that conform to Codable can be decoded from JSON (or other formats) and encoded back. Swift's JSONDecoder and JSONEncoder work with any Codable type.
When should I use struct vs class for Swift models?
Use structs for data models in most cases: they have value semantics (copies are independent), are safer in concurrent code, and gain Equatable and Hashable conformance automatically. Use classes when you need reference semantics, inheritance, or Objective-C interoperability.
What are CodingKeys?
CodingKeys is an enum that maps Swift property names to JSON key names. The generator adds CodingKeys automatically when JSON keys use snake_case (user_id) and you want camelCase Swift properties (userId). You can also use JSONDecoder.keyDecodingStrategy = .convertFromSnakeCase instead.
How does the generator handle nested JSON objects?
Each nested JSON object generates a nested Swift struct or class with the same name as its JSON key in PascalCase. These are declared as inner types of the parent struct for clean namespacing.
How are JSON arrays handled?
JSON arrays of objects generate an array property ([TypeName]) with a corresponding nested type definition. Arrays of primitives generate the appropriate Swift array type ([String], [Int], etc.).
What happens with null JSON values?
JSON fields with null values generate Optional properties in Swift (e.g., String?). This correctly models the possibility that the server may omit or null the field.
Is the generated code ready to use with URLSession?
Yes. The generated types can be used directly with JSONDecoder: let model = try JSONDecoder().decode(YourType.self, from: data). For snake_case keys, set decoder.keyDecodingStrategy = .convertFromSnakeCase before decoding.
Is this tool free?
Completely free. The conversion runs entirely in your browser: no data is sent to any server.

AlteredIdea vs alternatives

vs other online tools: Everything runs in your browser: private, instant, no account needed.

vs desktop apps: No install required. Works on any device.

vs paid tools: Completely free, unlimited use.