"category": "code-generator",

JSON to Swift Codable Generator

"tldr": Paste JSON and get Swift Codable structs - camelCase properties with CodingKeys mapping the original snake_case keys automatically.

Paste a JSON payload and get Swift structs conforming to Codable, ready for JSONDecoder. Properties are camelCased, and when the JSON keys differ (snake_case APIs), a CodingKeys enum is generated automatically so decoding just works.

{"json": "swift struct"}

Codable is the standard way iOS and macOS apps consume APIs - hand-writing the structs and CodingKeys for a deep payload is pure boilerplate, and a single misspelled key means a decoding error at runtime. Generating from a real response removes that whole class of bugs.

How to convert JSON to Swift Codable structs

  1. 1Paste a representative JSON payload into the input panel.
  2. 2Properties are camelCased; a CodingKeys enum is added only when names differ from the JSON.
  3. 3Nested objects become their own Codable structs.
  4. 4Copy into Xcode and decode with JSONDecoder().decode(Root.self, from: data).

Convert JSON to Swift struct in code

Decode with JSONDecoder
let decoder = JSONDecoder()
let order = try decoder.decode(Root.self, from: data)
print(order.customer.name, order.total)
From a URLSession response
let (data, _) = try await URLSession.shared.data(from: url)
let order = try JSONDecoder().decode(Root.self, from: data)

Frequently asked questions

Can I skip CodingKeys with a decoder setting?

Yes - JSONDecoder has keyDecodingStrategy = .convertFromSnakeCase, which maps order_id to orderId automatically. The generated CodingKeys are explicit and survive unusual keys; the strategy is less code. Pick one approach per project, not both.

How do optional fields work with Codable?

Declare them as optionals: let notes: String?. JSONDecoder maps both null and a missing key to nil for optional properties. Non-optional properties throw DecodingError.keyNotFound when the key is absent.

Should these be structs or classes?

Structs, almost always - value semantics, automatic memberwise initializers, and free Codable synthesis. Use a class only if you need reference identity or inheritance, and then you may need to implement init(from:) manually.

What about dates?

ISO 8601 strings decode to Date by setting decoder.dateDecodingStrategy = .iso8601 and declaring the property as Date instead of String. Unix timestamps use .secondsSince1970. The generator emits String for date-looking values - upgrade them once you set the strategy.

Last updated:

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →