Paste any JSON and instantly generate Swift 5 structs with Codable conformance and CodingKeys. Free, private, no signup required.
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.
Swift's Codable protocol (combining Encodable + Decodable) enables type-safe JSON parsing with minimal boilerplate. It's the official Apple approach for JSON in Swift and iOS development.
// JSON
{"userId": 1, "name": "Alice", "isActive": true, "score": 98.5}
// Swift Struct
import Foundation
struct User: Codable {
let userId: Int
let name: String
let isActive: Bool
let score: Double
enum CodingKeys: String, CodingKey {
case userId = "userId"
case name
case isActive = "isActive"
case score
}
}
// Decode from JSON Data
let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let user = try decoder.decode(User.self, from: jsonData)
print(user.name) // Alice
// Encode to JSON
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let encoded = try encoder.encode(user)
| Option | Description |
|---|---|
| keyDecodingStrategy = .convertFromSnakeCase | snake_case JSON → camelCase Swift automatically |
| dateDecodingStrategy = .iso8601 | Parse ISO 8601 date strings to Date |
| nonConformingFloatDecodingStrategy | Handle NaN/Infinity in JSON floats |
| outputFormatting = .prettyPrinted | Readable JSON output when encoding |
This example shows the full workflow from JSON to generated Swift structs with decode and encode operations.
{
"id": 1,
"firstName": "Alice",
"email": "alice@example.com",
"address": {"city": "London", "postcode": "SW1A 1AA"},
"scores": [95, 87, 92],
"active": true
}
// Generated Swift structs
struct Address: Codable {
let city: String
let postcode: String
}
struct User: Codable {
let id: Int
let firstName: String
let email: String
let address: Address
let scores: [Int]
let active: Bool
}
// Decode from JSON
let decoder = JSONDecoder()
let user = try decoder.decode(User.self, from: jsonData)
print(user.firstName) // "Alice"
// Encode back to JSON
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let outputData = try encoder.encode(user)
let jsonString = String(data: outputData, encoding: .utf8)!
// JSON uses snake_case, Swift uses camelCase
// Use CodingKeys to map them
struct User: Codable {
let userId: Int
let firstName: String
let createdAt: Date
enum CodingKeys: String, CodingKey {
case userId = "user_id"
case firstName = "first_name"
case createdAt = "created_at"
}
}
// Automatic snake_case conversion (simpler)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let user = try decoder.decode(User.self, from: jsonData)
// Also handle dates
decoder.dateDecodingStrategy = .iso8601
Also useful: JSON to TypeScript | JSON to Kotlin | JSON to Rust | JSON to Java | JWT Decoder