Paste any JSON and instantly generate Swift 5 structs with Codable conformance and CodingKeys. Free, private, no signup required.
Open JSON to Swift Converter →Given this JSON from an API response:
{
"user_id": 42,
"full_name": "Jane Smith",
"email": "jane@example.com",
"is_active": true,
"address": {
"street": "123 Main St",
"city": "San Francisco"
}
}
The converter generates:
struct RootModel: Codable {
let userId: Int
let fullName: String
let email: String
let isActive: Bool
let address: Address
enum CodingKeys: String, CodingKey {
case userId = "user_id"
case fullName = "full_name"
case email
case isActive = "is_active"
case address
}
}
struct Address: Codable {
let street: String
let city: String
}
| JSON Type | Swift Type | Notes |
|---|---|---|
| string | String | Direct mapping |
| integer number | Int | Whole numbers |
| decimal number | Double | Floating point values |
| boolean | Bool | true / false |
| null | Type? | Made optional |
| array | [Type] | Array of inferred type |
| object | Nested struct | Named after key |
JSONDecoder to parse your API response datadecoder.keyDecodingStrategy = .convertFromSnakeCase to avoid CodingKeys entirely? if they may be absent from the responsedecoder.dateDecodingStrategy to match your API's date formatlet decoder = JSONDecoder()
let user = try decoder.decode(RootModel.self, from: jsonData)
Prefer struct for JSON models. Structs are value types, which means they are copied rather than referenced, making them safer for data models. They also work perfectly with Codable. Use class only when you need inheritance or reference semantics.
If a JSON field may be absent or null, declare it as optional in Swift: let field: String?. JSONDecoder will set it to nil if the key is missing or the value is null. The converter marks fields with null values as optional automatically.
Yes. The generated Codable structs work in any Swift environment: SwiftUI, UIKit, Vapor (server-side Swift), Swift Package Manager libraries, and macOS apps. The code requires Swift 4.1 or later.
For JSON objects with dynamic keys, use [String: SomeType] in Swift instead of a struct. For mixed-type values, use [String: AnyCodable] with a third-party AnyCodable wrapper, or decode using a custom init(from:) implementation.
Free, instant, 100% private. No account needed.
Open JSON to Swift Converter →Also useful: JSON to TypeScript | JSON to Kotlin | JSON to Rust | JSON to Java | JWT Decoder