JSON to Swift Codable generator

Paste a JSON response and get Swift structs that decode it with JSONDecoder. Keys become camelCase properties, and a struct whose keys differ gets a CodingKeys enum with the exact JSON names. Values that can be null or missing become optionals.

Properties

Runs in your browser: nothing you enter is sent anywhere.

How to use it

  1. Paste a JSON sample with realistic data; several items per array help find optional keys.
  2. Choose let (immutable) or var properties.
  3. Copy the code into a .swift file and decode with try JSONDecoder().decode(Root.self, from: data).

How it works

The sample is read into a shape: every key with every type seen there. All objects of one array become one struct, so a key present in only some of them becomes optional. Codable decodes a missing optional key as nil.

  • Property names are camelCase: order_id → orderId; a Swift keyword such as default is written in backticks.
  • Struct names come from keys: items holds [Item], customer becomes Customer.
  • A top-level array gives a typealias, such as typealias Root = [RootItem].

Worked example

{"line_items":[{"sku":"A","note":null,"qty":1},{"sku":"B","note":"x"}]} with var gives:

import Foundation

struct Root: Codable {
    var lineItems: [LineItem]

    enum CodingKeys: String, CodingKey {
        case lineItems = "line_items"
    }
}

struct LineItem: Codable {
    var sku: String
    var note: String?
    var qty: Int?
}

Tips

  • Dates arrive as strings: change the type to Date and set decoder.dateDecodingStrategy (for example .iso8601) if you want them parsed.
  • Add Identifiable or Hashable conformance yourself where SwiftUI needs it.
  • For Kotlin or TypeScript models of the same JSON, use the neighbouring generators.

Frequently asked questions

How are JSON types mapped to Swift?

Strings become String, true and false Bool, whole numbers Int and other numbers Double (a mix of both becomes Double). Arrays become [T], objects become their own Codable structs, and null or a key missing from some objects makes the property optional (T?).

When is a CodingKeys enum added?

When at least one property name differs from its JSON key, for example orderId for order_id. Swift then needs a case for every property, so the enum lists them all, with the JSON name only where it differs.

What is JSONValue?

Codable has no built-in type for "any JSON". When a place holds different kinds of values, the generator uses a small enum JSONValue (string, number, bool, object, array, null) and appends its Codable code to the output.

Can I use convertFromSnakeCase instead?

Yes: with decoder.keyDecodingStrategy = .convertFromSnakeCase you can delete the CodingKeys enums for snake_case keys. Keep them if keys use other styles, such as first-name, that the strategy does not convert.

More in Developer

All tools in this category: Developer ·Updated November 4, 2026

Privacy choices

Google Analytics shows us which tools are used and whether they work. It may set cookies. Your choice is kept in this browser.