JSON to Swift Converter Online — Generate Codable Struct

Generate Swift structs from any JSON payload — automatic Codable conformance, CodingKeys hints, nested structs, and JSONDecoder-ready output for iOS, macOS, SwiftUI, and server-side Swift. 100% in your browser.

What is a JSON to Swift converter?

A JSON to Swift converter reads a JSON sample and emits typed Swift structs conforming to Codable that mirror the JSON shape — value-type properties, nested structs for nested objects, and CodingKeys hints for snake_case JSON keys. The output drops straight into an iOS, macOS, watchOS, tvOS, SwiftUI, or Vapor project and (de)serializes with JSONDecoder and JSONEncoder from Foundation.

Writing struct definitions by hand from a 200-field API payload wastes hours and is bug-prone — a single typo in a property name silently throws DecodingError.keyNotFound at runtime. OpenFormatter's converter walks the JSON tree once, generates idiomatic Swift, and runs entirely in your browser so internal API responses never leave your machine.

How to generate Swift Codable structs from JSON — 4 steps

  1. Paste a real JSON response. Use a representative sample — type inference quality depends on it. Booleans must be true/false (not 0/1), numbers must be unquoted.
  2. Click Convert. The tool emits a Root struct plus one separate struct per nested object, all conforming to Codable.
  3. Copy and paste into your Xcode project. Each struct is independent — split them across files in your Models group.
  4. Decode. let root = try JSONDecoder().decode(Root.self, from: data) — that is all Foundation needs once the struct is Codable.

JSON to Swift type mapping

Sample JSON

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "score": 98.5,
  "address": { "city": "London" },
  "tags": ["admin", "engineer"]
}

Generated Swift Codable struct

import Foundation

struct Root: Codable {
    var id: Int
    var name: String
    var active: Bool
    var score: Double
    var address: Address
    var tags: [String]
}

struct Address: Codable {
    var city: String
}

Codable conformance

Each struct conforms to Codable so JSONDecoder/JSONEncoder work without writing init(from:) or encode(to:) by hand. Drop in, decode, done.

CodingKeys ready

For snake_case JSON keys add either a CodingKeys enum inside the struct or set keyDecodingStrategy = .convertFromSnakeCase on the JSONDecoder.

Client-Side Only

JSON is parsed in JavaScript inside the browser. Internal iOS API responses, tokens, or PII never reach a server.

Common use cases

  • check_circleGenerating iOS REST API model structs from a real JSON response
  • check_circleBuilding SwiftUI view-model types from a backend JSON payload
  • check_circleProducing async/await URLSession decoder targets
  • check_circleModelling Combine dataTaskPublisher decode pipelines
  • check_circleGenerating Vapor server route content (request and response) types
  • check_circleModelling CoreData JSON imports / exports
  • check_circleBuilding shared Apple Watch and iPhone communication payloads
  • check_circleModelling Stripe, Firebase, GitHub or any third-party SaaS webhook payload

Why client-side generation matters

The JSON you paste often contains real customer data, OAuth tokens, signed URLs, or internal field names you do not want indexed by a third-party converter. OpenFormatter generates the Swift entirely in JavaScript on your device — open the Network tab in DevTools and you will see zero requests when you click Convert. Safe for enterprise use, safe behind a corporate proxy, safe when the API contract is itself confidential.

Need other Apple-platform tooling?

Format JSON, validate JSON, or generate types for Android and Flutter — all browser-side.

Frequently Asked Questions

Does it produce Codable?

Yes. Every generated struct is declared as struct Name: Codable, which is Swift's typealias for Decodable & Encodable. That means you can JSONDecoder().decode(Root.self, from: data) to read and JSONEncoder().encode(root) to write — both directions for free, no extra protocols or boilerplate. If you only need decoding (the common case for API consumers), narrow the conformance to Decodable; for write-only DTOs use Encodable.

How are nested JSON keys handled with CodingKeys?

For JSON keys that are valid Swift identifiers and match the Swift property name, no CodingKeys enum is needed — Codable maps them automatically. For snake_case JSON keys (e.g. zip_code) you have two options: (1) set decoder.keyDecodingStrategy = .convertFromSnakeCase to remap the entire payload to camelCase Swift properties, or (2) declare a private enum CodingKeys: String, CodingKey { case zipCode = "zip_code" } inside the struct to map a single property. Option 1 is simplest for full snake_case APIs; option 2 keeps the rest of the JSON unchanged.

How do I deserialize the generated struct?

let decoder = JSONDecoder(); let root = try decoder.decode(Root.self, from: data) — that is everything Foundation needs once Root: Codable is declared. To handle ISO 8601 dates, set decoder.dateDecodingStrategy = .iso8601 before decoding. To map snake_case keys automatically, set decoder.keyDecodingStrategy = .convertFromSnakeCase.

Can I use this with SwiftUI and Combine?

Yes. The generated Codable struct is a value type so it composes safely with SwiftUI @State and @Published properties. With Combine: URLSession.shared.dataTaskPublisher(for: url).map(\.data).decode(type: Root.self, decoder: JSONDecoder()) gives you a Publisher<Root, Error> for a single line of pipeline code. With async/await: let (data, _) = try await URLSession.shared.data(from: url); let root = try JSONDecoder().decode(Root.self, from: data).

How are nested JSON objects modelled?

Each nested object becomes its own top-level Swift struct also conforming to Codable so you can split them into separate .swift files. The parent struct holds a typed reference (var address: Address) and JSONDecoder recurses into the nested struct automatically.

How are JSON arrays of objects mapped?

Arrays of objects become [StructName] (Swift's shorthand for Array<StructName>). Arrays of primitives become [String], [Int], [Double], or [Bool]. JSONDecoder handles Swift arrays natively — no custom decoder required.

How are nullable / optional fields handled?

JSON null values produce Any? as a safe default. To express an optional field, change var name: String to var name: String? after copying — JSONDecoder then accepts both null and missing keys (as long as the field is declared optional) without throwing.

Is the JSON uploaded to your servers?

No. Conversion runs entirely in your browser via JavaScript. Open DevTools → Network and click Convert — no requests are made. JSON containing API keys, internal field names, or PII never leaves your machine.

JSON to Swift Converter — Generate Codable Struct