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
Swift mapping notes
- Every struct conforms to
Codable, soJSONDecoderandJSONEncoderwork with no extra code. - Keys that aren't Swift-style camelCase (
full_name,repoId) get aCodingKeysenum that maps the Swift name back to the JSON key. - Integers map to
Int, decimals toDouble, ISO 8601 strings toDate; 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(...)returnsnilwith no reason. Usedo { try ... } catch { print(error) }while developing;DecodingErrornames the exact key and path that failed.- Dates need a strategy. A plain
JSONDecoder()cannot read ISO 8601 strings, so anyDatefield fails until you set.iso8601. That strategy rejects fractional seconds (.123Z); for those, use a customISO8601DateFormatterwith.withFractionalSeconds. - Fields missing from your sample. A non-optional property makes decoding throw
keyNotFoundwhen the API omits it. Paste several objects so sometimes-missing keys become optionals. - Int vs Double. A field that is
1in your sample becomesInt; if the API sends1.5, decoding fails. Include a decimal in the sample or change the type toDouble.
How to use
- Paste a JSON sample (an API response works well) into the Input pane.
- Generated Swift structs appear instantly. Nested objects become their own named types.
- Non-standard JSON (single quotes, trailing commas, comments) is auto-repaired first.
- Copy the code into your project.
Examples
JSON → Swift structs
{
"id": 42,
"name": "workbench",
"owner": { "email": "[email protected]", "active": true }
}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.