JSON to Swift
Paste a JSON sample and get Swift Codable structs, instantly and entirely in your browser. Optional fields, nullability and nested types are inferred from the sample — and anything the sample cannot settle is reported rather than guessed.
Type definitions will appear here
Swift codable structs from a sample
Generates struct ... : Codable declarations. A CodingKeys enum is emitted only when a property name differs from its JSON key, rather than always.
Optional and nullable fields are typed T?.
What a sample cannot tell you
Types are inferred from one sample. A field absent from your sample will be absent from the output, and a field that happens to hold only whole numbers will be typed as an integer even if it can hold decimals. Paste the widest sample you have. For a guarantee rather than an inference, start from a JSON Schema.
What Swift output looks like
Generated from two sample records, one of which has an extra field and a null. This is the actual output, not an illustration:
import Foundation
// MARK: - Root
struct Root: Codable {
let id: Int64
let userName: String
let isActive: Bool
let score: Double
let tags: [String]
let address: Address
let lastSeen: String
let nickname: String?
}
// MARK: - Address
struct Address: Codable {
let city: String
let postcode: JSONAny?
}
// A stand-in for a value whose type the sample could not settle.
struct JSONAny: Codable {}
The three things that differ by language
Large integers
Swift has Int64, so large identifiers survive. On a 64-bit platform plain Int would also hold them, but Int64 is emitted explicitly so the intent is not platform-dependent.
Optional against nullable
Optional and nullable fields become T?. Codable decodes a missing key into nil for an optional property, and throws for a non-optional one — so the distinction is load-bearing.
Names that are not identifiers
A CodingKeys enum is emitted only when a property name differs from its key, rather than always. Most generators emit it unconditionally, which is noise.