{}JSONLint.app

JSON to Swift Converter

Generate Swift structs from JSON. Paste your JSON:

Loading...
Loading...

Generate Swift Codable Structs

This tool converts JSON to Swift structs conforming to the Codable protocol. It automatically generates CodingKeys for property name mapping.

Example Output

struct User: Codable {
    let id: Int
    let userName: String
    let isActive: Bool

    enum CodingKeys: String, CodingKey {
        case id
        case userName = "user_name"
        case isActive = "is_active"
    }
}

Type Mapping

JSONSwift
stringString
integerInt
decimalDouble
booleanBool
nullString?
array[T]
objectNested struct

Using Generated Code

let jsonData = """
{"id": 1, "user_name": "john"}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let user = try decoder.decode(User.self, from: jsonData)
print(user.userName) // "john"

Related Tools

Common Mistakes & Pro Tips

  • CodingKeys are how you map non-matching JSON keysCodable matches property names to JSON keys exactly, so a snake_case key like `created_at` won't bind to `createdAt`. Either set `decoder.keyDecodingStrategy = .convertFromSnakeCase`, or add a `CodingKeys` enum mapping each property to its JSON key (`case createdAt = "created_at"`). The generator usually emits a `CodingKeys` enum for you.
  • Optionals are required for missing or null keysA non-optional `Codable` property throws a decoding error if the key is missing or `null`. Declare any field that may be absent or null as an optional (`String?`), which decodes a missing/null value to `nil` automatically. Synthesized `init(from:)` treats optionals as decode-if-present.
  • Int vs Double and the Int sizeWhole numbers map to `Int`, decimals to `Double`. `Int` is 64-bit on all modern Apple platforms, so large IDs are fine, but a value with no decimal point will fail to decode into a `Double` only if you mismatch types — keep types consistent with the data. For money use `Decimal` to avoid `Double` rounding; note `Decimal` decodes from JSON numbers.
  • Empty arrays, mixed arrays, and datesAn empty array `[]` gives no element type — the generator falls back to a placeholder you must replace with the real element type. Heterogeneous arrays don't map cleanly to a typed `[T]` and may need a custom enum or `AnyCodable`. Date strings stay `String` unless you set `decoder.dateDecodingStrategy = .iso8601` and type the property `Date`.
  • Reserved words and snake_case strategy interplayJSON keys that are Swift keywords (`default`, `class`, `protocol`) need backticks (`` `default` ``) or a renamed property with a `CodingKeys` mapping. If you use `.convertFromSnakeCase`, don't also add manual snake_case `CodingKeys` for the same field, or the conversions can conflict — pick one approach per property.

Frequently Asked Questions

Why do my structs conform to Codable?

`Codable` is a typealias for `Decodable & Encodable`, and conforming lets `JSONDecoder().decode(MyType.self, from: data)` build your struct and `JSONEncoder` turn it back into JSON. The compiler synthesizes the encoding/decoding automatically as long as every stored property is itself `Codable`. That's why nested types must also be `Codable`.

How do I handle optional or nullable fields?

Make the property optional with `?` (e.g. `var nickname: String?`). The synthesized decoder uses `decodeIfPresent` for optionals, so a missing key or explicit `null` decodes to `nil` without throwing. Non-optional properties, by contrast, throw `keyNotFound` or `valueNotFound` if the key is absent or null.

How do I decode snake_case JSON into camelCase properties?

Set `decoder.keyDecodingStrategy = .convertFromSnakeCase` to map `created_at` to `createdAt` globally, which avoids writing `CodingKeys` for every field. Alternatively, provide an explicit `CodingKeys` enum per type for full control or for keys the automatic strategy can't handle (like acronyms). Use one approach consistently to avoid surprises with mixed-case keys.

Should I generate `struct` or `class`?

Prefer `struct` for JSON models — value semantics, immutability with `let`, and thread-safety make them the Swift default, and they conform to `Codable` cleanly. Use a `class` only when you need reference semantics, inheritance, or identity (e.g. Core Data / SwiftData or shared mutable state). Most API DTOs should be structs.

How do I parse ISO 8601 dates?

JSON has no date type, so timestamps arrive as strings. Type the property as `Date` and set `decoder.dateDecodingStrategy = .iso8601` for standard ISO 8601 strings, or use `.formatted(DateFormatter)` / `.custom` for non-standard formats and fractional seconds. Without a strategy, keep the field as `String`.

Is my JSON uploaded to generate the Swift code?

No — the tool runs entirely in your browser, so the JSON you paste is converted locally and never sent over the network. This is safe for internal API responses or anything containing tokens, and it works offline.