JSON to Swift Struct
Generate Codable Swift structs from JSON, free, in your browser.
What it's for
From JSON to Swift Codable Struct
Ready for iOS and SwiftUI
Generates structs compatible with URLSession, Alamofire, SwiftUI, and Combine. Paste directly into Xcode.
100% private
Your API JSON never leaves your browser. No signup, no telemetry.
Correct Codable
Automatic CodingKeys for snake_case, Optional for nullables, correct types (Int, Double, Bool). Idiomatic code.
Instant
Structs generated in real time as you type. No buttons, no waiting.
How it works
Three steps, no hassle
Paste your JSON
Paste the JSON from your API or web response. Works with nested objects, arrays, and any valid JSON structure.
Instant generation
The tool generates Swift structs conforming to the Codable protocol automatically, with CodingKeys when needed.
Copy the code
Copy the generated structs and use them directly in your iOS, macOS, or SwiftUI project.
FAQ
Got questions?
Codable is a type alias introduced in Swift 4 (WWDC 2017) that combines two protocols: Encodable (can be converted to an external format like JSON) and Decodable (can be created from an external format). A type conforming to Codable can both serialize and deserialize data. Conformance is obtained automatically via compiler synthesis if all properties also conform to Codable.
For JSON data models, structs are preferred for several reasons: structs are value types (they copy rather than reference), avoiding shared state issues; the compiler can optimize them better; and in SwiftUI, structs work better with the reactive data model. Classes (reference types) have their place when you need inheritance or object identity, but for API models structs are the recommended practice.
CodingKeys is a nested enum that maps Swift property names to key names in the JSON. It is needed when JSON names (typically snake_case in REST APIs) differ from Swift conventions (camelCase). For example, if the JSON has first_name, the CodingKey maps that key to the firstName field of the struct. Without CodingKeys, Swift expects the JSON names to match the property names exactly.
In Swift, an Optional type (marked with ?) can have a value or be nil. When a JSON field can be absent or null, the struct property is declared as Optional: var email: String? instead of var email: String. Foundation's JSONDecoder automatically handles deserialization: if the key is absent from the JSON or its value is null, the property is set to nil without throwing an error.
The typical URLSession + Codable flow is: URLSession.shared.dataTask returns Data; JSONDecoder().decode(MyStruct.self, from: data) converts the Data to the struct; the result is used in the UI on the main thread. With Swift 5.5+ and async/await, use let data = try await URLSession.shared.data(for: request) followed by let object = try JSONDecoder().decode(MyStruct.self, from: data). This pattern is the foundation of all API integration in modern iOS.
Swift for iOS/macOS, the Codable protocol (2017), and API integration with SwiftUI
Swift was introduced by Apple at WWDC 2014 as the successor to Objective-C for app development on iOS, macOS, watchOS, and tvOS. With the introduction of Codable in Swift 4 (WWDC 2017), working with JSON was radically simplified. Before Codable, developers used third-party libraries like SwiftyJSON or Alamofire's Codable extensions. With native Codable, you only need JSONDecoder and well-typed structs, with no external dependencies.
SwiftUI, introduced at WWDC 2019, reinforced the use of structs as data models. SwiftUI's architecture based on value types and the View protocol as a struct fits perfectly with Codable models. Patterns like MVVM (Model-View-ViewModel) in SwiftUI use @Published in ObservableObject ViewModels with Codable structs as models, creating a predictable and testable data flow.
With Swift 5.5 (WWDC 2021) and the introduction of async/await, consuming REST APIs became even cleaner. URLSession.shared.data(for:) is now an async function that eliminates nested completion handlers (callback hell). Combined with JSONDecoder, the complete fetch + decode pattern is just 3 lines of code. This tool generates the Codable structs needed for that pattern, processing everything in your browser without sending your data to any server.