>_devtools

JSON to Swift Codable Converter

Generate Swift Codable structs or classes from a JSON sample, with access level and CodingKeys options.

What this does

Generates Swift struct (or class) declarations from a JSON sample. Turn Types only off to make them Codable and addCodingKeys, so JSONDecoder can read your payload directly.

Example

Input

{
  "id": 42,
  "name": "Ada",
  "verified": true,
  "tags": [
    "admin",
    "dev"
  ],
  "orders": [
    {
      "sku": "A1",
      "qty": 2
    },
    {
      "sku": "B2",
      "qty": 1,
      "note": "gift"
    }
  ]
}

Output with default options

import Foundation

// MARK: - Root
struct Root {
    let id: Int
    let name: String
    let verified: Bool
    let tags: [String]
    let orders: [Order]
}

// MARK: - Order
struct Order {
    let sku: String
    let qty: Int
    let note: String?
}

How values are mapped

  • Whole numbers become Int, other numbers Double, strings String, booleans Bool.
  • Arrays become [T]; a field missing from some objects becomes T?.
  • ISO 8601 timestamps become Date when date detection is on.
  • Arrays of mixed types become an enum with an associated value per type.

Options

Choose struct or class, internal or public access, andlet or var properties. Generate CodingKeys maps Swift-style names back to the original JSON keys when you want a Codable result.

Things to know

Property names become lowerCamelCase, so user_id turns intouserID. Swift has no way to decode a bare null value into a useful type, so a field that is always null becomes NSNull; replace it by hand once you know the real type.

Privacy

Generation runs in a Web Worker in your browser. The JSON is never uploaded.