JSON to Swift Codable Struct Generator

Generate Swift structs conforming to Codable with CodingKeys for iOS/macOS development.

  • 100% free
  • Private in browser
  • No signup
  • No watermarks

Swift Codable Structs (1)

import Foundation

struct ProductModel: Codable {
    let itemId: Int
    let name: String
    let price: Double

    enum CodingKeys: String, CodingKey {
        case itemId = "item_id"
        case name
        case price
    }
}

About JSON to Swift Codable Struct Generator

The JSON to Swift Struct Converter generates Codable structs from a sample JSON payload for iOS and macOS apps. Paste an API response, set the root struct name, and the tool writes camelCase let properties and adds a CodingKeys enum whenever a property name differs from its JSON key. Whole numbers become Int, decimals Double, booleans Bool, and arrays become [T]. Null values and empty arrays are guessed as String? and [String], so correct those after checking your real data.

How to use JSON to Swift Codable Struct Generator

  1. Enter a root struct name, such as ProductModel.
  2. Paste your sample JSON into the Input JSON box.
  3. Review the properties and CodingKeys in the Swift Codable Structs panel.
  4. Click Copy Swift Code and decode the type with JSONDecoder in your app.

Key features

  • Structs conform to Codable with immutable let properties
  • CodingKeys enum generated only when a key needs renaming
  • Maps whole numbers to Int, decimals to Double, and booleans to Bool
  • Creates a separate struct for each nested object
  • Types arrays as [T] based on their first element
  • Converts snake_case and kebab-case keys to camelCase property names

Frequently asked questions

How do I convert JSON to a Swift struct?
Paste your JSON into the Input JSON box, set a root struct name, and the Codable structs are generated instantly. Click Copy Swift Code to copy them into Xcode.
When does the tool add a CodingKeys enum?
Only when at least one property name differs from its JSON key. A key like item_id becomes let itemId with case itemId = "item_id". If every key is already camelCase, no CodingKeys enum is written.
How are null values and empty arrays typed?
A null value becomes String? and an empty array becomes [String]. These are guesses, since the sample carries no type, so change them to the real type, such as Int? or [Review], before decoding.
How do I decode JSON with the generated struct?
Call JSONDecoder, for example try JSONDecoder().decode(ProductModel.self, from: data). If the root of your JSON is an array, decode [ProductModel].self; the tool builds the struct from the first array element.
How do I mark a property as optional?
Add ? to the type yourself, for example let price: Double?. The generator makes every property non-optional unless it is null in the sample, so a missing key throws keyNotFound when decoding.

Rate this tool

Be the first to rate — helps others and improves this page.