JSON to Swift Codable struct

Built for iOS and macOS apps: the output decodes directly with JSONDecoder.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter json input and root type name.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

String, Int, Double, Bool and [T], with a Codable struct per object. When a camelCase property name differs from its key, CodingKeys are added, and a field that can be null or missing becomes an optional T?. Mixed values use a JSONValue enum attached to the output.

Example

Inputs: Root type name = Root. Result: Generated code: import Foundation struct Root: Codable { let id: Int let name: String let email: String let isActive: Bool let balance: Double let tags: [String] let address: Address let orders: [Order] enum CodingKeys: String, CodingKey { case id case name case email case isActive = "is_active" case balance case tags case address case orders } } struct Address: Codable { let city: String let postalCode: String enum CodingKeys: String, CodingKey { case city case postalCode = "postal_code" } } struct Order: Codable { let orderId: String let total: Double let coupon: String? enum CodingKeys: String, CodingKey { case orderId = "order_id" case total case coupon } }; Types generated: 3; Fields: 13.

{
  "id": 1024,
  "name": "Sara Ahmed",
  "email": "sara@example.com",
  "is_active": true,
  "balance": 1520.75,
  "tags": ["vip", "riyadh"],
  "address": { "city": "Riyadh", "postal_code": "12211" },
  "orders": [
    { "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
    { "order_id": "A-2", "total": 120, "coupon": null }
  ]
}
import Foundation

struct Root: Codable {
    let id: Int
    let name: String
    let email: String
    let isActive: Bool
    let balance: Double
    let tags: [String]
    let address: Address
    let orders: [Order]

    enum CodingKeys: String, CodingKey {
        case id
        case name
        case email
        case isActive = "is_active"
        case balance
        case tags
        case address
        case orders
    }
}

struct Address: Codable {
    let city: String
    let postalCode: String

    enum CodingKeys: String, CodingKey {
        case city
        case postalCode = "postal_code"
    }
}

struct Order: Codable {
    let orderId: String
    let total: Double
    let coupon: String?

    enum CodingKeys: String, CodingKey {
        case orderId = "order_id"
        case total
        case coupon
    }
}

Limits and caveats

  • Properties use let; change them to var if you need to modify values.
  • Swift Int is 64-bit on current devices.

Questions about this tool

When are CodingKeys added?

When at least one property name differs from its JSON key, such as postal_code becoming postalCode.

What is JSONValue in the output?

A small enum that decodes any JSON value, used for keys with mixed types or only null values.

Can I use convertFromSnakeCase instead?

Usually yes, but CodingKeys are explicit and also handle keys with dashes or spaces.