TXT · Text & Data tools

JSON to Swift Struct Converter

Top-level type
Kind
Access
Conformance
Acronyms
Type prefix
JSON input
Swift output
Inference
SwiftlanguageJSON sampleinput0types0lines out0chars out
Go to the full JSON to Types generator with all ten languages

Shareable link

Settings are written to the URL as you change them. Nothing differs from the defaults yet.

Swift types from a JSON sample

Paste a JSON document and get Codable structs for it. Each object shape becomes a struct, arrays become [T], and a CodingKeys enum pins every property to the key it came from.

Example

import Foundation

struct Customer: Codable {
    let id: Int
    let name: String
    let orders: [Order]
}

struct Order: Codable {
    let sku: String
    let qty: Int
    let gift: Bool?
}

gift is Bool? because only some orders in the sample carry it. With Initializers on, each struct also gets init(data:), init(_ json:), and jsonData() built on JSONDecoder and JSONEncoder.

Options

Kind switches between struct and class. var instead of let makes properties mutable. Access sets internal or public on every declaration, for types shipped in a framework. Conformance adds Equatable or Hashable alongside Codable. Sendable marks the types for Swift concurrency. Type prefix prepends a string to every type name, the old Objective-C habit, when the generated names would collide with ones in your module.

Limits

Swift has no anonymous union type, so a field that mixes types in the sample becomes an enum with one associated-value case per type and a hand-written init(from:). Repeated string values become a String-backed enum when enum inference is on. Dates become Date only when date inference is on, and the decoder in the generated initializers is configured for ISO 8601.

Frequently Asked Questions

Yes. Every struct or class conforms to Codable, with a CodingKeys enum that maps each property to its JSON key. Conformance adds Equatable or Hashable on top.

Structs by default, which is the Swift convention for value data. Set Kind to class when you need reference semantics or inheritance; final classes can then be marked final.

It writes the CodingKeys enum even when every property name matches its JSON key. With it off, the enum is only written where a name differs, such as a snake_case key.

A key missing from some objects becomes an optional, such as Bool?, which JSONDecoder leaves nil when the key is absent or null.

A memberwise init plus init(data:) and init(_ json:) convenience initializers that decode from Data or a String, and a jsonData() method that encodes back. Uncheck to generate the bare types.

Generate types for one language

Full JSON to Types Generator tool

Explore Our Tools

Browse all tools