Skip to content
ZeroServer.tools

JSON to Swift Struct

Generate a Swift struct with Codable conformance from a JSON sample.

or drop a file here
Swift struct
import Foundation

struct Root: Codable {
    let id: Int
    let name: String
    let email: String
    let score: Double
    let active: Bool
    let tags: [String]
    let address: Address
}

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

How JSON to Swift struct generation works

JSON types map to Swift: strings → String, integers → Int, decimals → Double, booleans → Bool, arrays → [T], and null values → optional types. All structs conform to Codable (bothEncodable and Decodable). CodingKeysare generated automatically when JSON keys don't match Swift camelCase naming.

For Kotlin equivalents, try JSON to Kotlin. For TypeScript, see JSON to TypeScript.

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Generating Codable structs from an API response instead of typing them out.
  • Getting Codable conformance written for you rather than hand-rolling init(from:).
  • Seeing how a payload's nesting maps onto nested Swift types.
  • Producing decodable models for a preview or a mock that must compile against the real ones.
  • Getting CodingKeys generated for keys that are not valid Swift identifiers.

Frequently Asked Questions

What Swift types does it infer?
`Bool` for booleans, `String` for strings, and for numbers it splits on whether the value is a whole number — 98.5 becomes `Double`, 1 becomes `Int`. A JSON null carries no type information at all, so it emits `String?` as the least-wrong guess and expects you to correct it.
How does it type an array of mixed values?
By unifying across EVERY element rather than trusting the first one. `[1, null, 3]` does not become `[Int]` just because it starts with an integer, and a genuinely mixed array falls back to `[Any]`. Reading only `array[0]` is the classic bug in generators like this, and a leading null exposes it.
Why is Codable on the struct?
Because it is what makes the struct usable: `JSONDecoder().decode(User.self, from: data)` needs the type to conform. Codable synthesises both encoding and decoding automatically as long as every stored property is itself Codable, which the generated nested structs are.
What happens to snake_case keys?
The property is emitted in Swift's lowerCamelCase. To map it back at decode time, either add a `CodingKeys` enum or set `decoder.keyDecodingStrategy = .convertFromSnakeCase` — the second is one line and covers the whole payload, which is usually why it wins.
Should a field be optional?
If the API can omit it, yes — a non-optional property makes the ENTIRE decode throw when the key is missing, not just that field. A sample JSON cannot tell the generator which keys are guaranteed, so treat the output as a starting point and mark the optional ones with `?` yourself.

Common errors and gotchas

  • Accepting inferred optionality, which one sample cannot determine.
  • Letting a whole number become Int when another response returns a decimal.
  • Assuming snake_case keys map automatically, when they need either CodingKeys or a decoding strategy.
  • Shipping generated type names derived from keys, which rarely read idiomatically.
  • Treating a JSON number above 2^53 as safe, when the precision was already lost upstream.

Related Converters tools

Private & free — this tool runs entirely in your browser.

IndieKitShip your Next.js startup in days.affiliate