{ } JSON Workbench100% Private • Zero Server Uploads

"JSON to Swift":{ }

Paste a JSON sample and get clean Swift structs. Nested objects become named types, arrays become typed collections, and nullable fields are handled. Powered by quicktype's inference engine running 100% in your browser: nothing is uploaded, so real API payloads stay on your machine.

What is a JSON to Swift converter?

Hand-writing structs for an API response is slow and error-prone. This tool infers Swift structs from a JSON sample: nested objects become named types, arrays become typed collections, and null/missing values become optional fields. It runs quicktype's inference engine entirely in your browser, so real API payloads (often containing user data or credentials) never leave your machine.

Other languages from the same JSON

The same generator also writes TypeScript, Python, Go, Java, C#, Rust, Kotlin, Dart, Ruby, PHP, Pydantic, Zod from the same sample, and a JSON Schema if you need a language-neutral contract.

Swift mapping notes

  • Every struct conforms to Codable, so JSONDecoder and JSONEncoder work with no extra code.
  • Keys that aren't Swift-style camelCase (full_name, repoId) get a CodingKeys enum that maps the Swift name back to the JSON key.
  • Integers map to Int, decimals to Double, ISO 8601 strings to Date; nullable or missing values become optionals (String?).

Decode JSON with JSONDecoder and URLSession

let url = URL(string: "https://api.example.com/repos/42")!
let (data, _) = try await URLSession.shared.data(from: url)

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601   // needed if any field is a Date
let repo = try decoder.decode(Root.self, from: data)
print(repo.owner.email)

For a JSON array, paste an array sample: the generator emits typealias Root = [RootElement], so the same decode(Root.self, ...) call returns an array. Encoding back is try JSONEncoder().encode(repo).

Common Swift JSON pitfalls

  • try? hides errors. try? decoder.decode(...) returns nil with no reason. Use do { try ... } catch { print(error) } while developing; DecodingError names the exact key and path that failed.
  • Dates need a strategy. A plain JSONDecoder() cannot read ISO 8601 strings, so any Date field fails until you set .iso8601. That strategy rejects fractional seconds (.123Z); for those, use a custom ISO8601DateFormatter with .withFractionalSeconds.
  • Fields missing from your sample. A non-optional property makes decoding throw keyNotFound when the API omits it. Paste several objects so sometimes-missing keys become optionals.
  • Int vs Double. A field that is 1 in your sample becomes Int; if the API sends 1.5, decoding fails. Include a decimal in the sample or change the type to Double.

How to use

  1. Paste a JSON sample (an API response works well) into the Input pane.
  2. Generated Swift structs appear instantly. Nested objects become their own named types.
  3. Non-standard JSON (single quotes, trailing commas, comments) is auto-repaired first.
  4. Copy the code into your project.

Examples

JSON → Swift structs

Input
{
  "id": 42,
  "name": "workbench",
  "owner": { "email": "[email protected]", "active": true }
}
Output
import Foundation

// MARK: - Root
struct Root: Codable {
    let id: Int
    let name: String
    let owner: Owner
}

// MARK: - Owner
struct Owner: Codable {
    let email: String
    let active: Bool
}

FAQ

How do I convert JSON to Swift types?

Paste any JSON sample. The structs are inferred from the values and nesting, entirely in your browser. Nothing is uploaded.

How are nested objects handled?

Each distinct nested object becomes its own named type, referenced from the parent, arrays of objects included.

What about optional or null fields?

Fields that are null or missing in parts of the sample are typed as optional/nullable in the generated code.

Can I paste multiple samples?

Paste an array of objects: the type is inferred from the union of all items, which catches optional fields a single sample would miss.

Should I use CodingKeys or keyDecodingStrategy?

Use the generated CodingKeys. Don't also set decoder.keyDecodingStrategy = .convertFromSnakeCase: the strategy converts full_name to fullName before matching, so the CodingKeys raw value "full_name" no longer matches and decoding fails.

Struct or class?

Structs: value semantics fit decoded API data, and Codable synthesis works the same. Switch to final class only if you need reference identity.

Does it work with SwiftUI and SwiftData?

The structs work directly as SwiftUI state and view inputs. For SwiftData persistence, map them into your @Model classes after decoding.

Is my JSON uploaded?

No. Generation runs 100% in your browser via quicktype's engine. Nothing is uploaded.

Related tools