Generate Swift Structs
from JSON
Paste any JSON and instantly get Swift struct definitions conforming to Codable. Perfect for iOS, macOS, and Swift API clients.
Open JSON to Swift ConverterSwift struct definitions in four steps
No account. No upload. No nonsense.
No Server
Your JSON never leaves your device. There is no backend to send it to.
iOS Ready
Generated structs conform to Codable and work with JSONDecoder out of the box for Swift 5.0+.
Works Offline
Load once and use forever — even on a plane or without internet access.
Related JSON tools
Common questions answered
Is JSON to Swift conversion free?
Yes, completely free with no signup required.
What Swift version is supported?
The output works with Swift 5.0+ and conforms to both Encodable and Decodable via the Codable typealias.
Does my data leave my browser?
No. All conversion is 100% client-side.
Are CodingKeys generated?
CodingKeys are generated when JSON keys contain characters not valid in Swift identifiers (e.g. hyphens, spaces).
How are optional values handled?
Fields that may be absent or null become Optional types (e.g., String? or Int?).
Converting JSON to Swift Codable structs
Swift Codable protocol provides built-in JSON encoding and decoding. By defining a struct conforming to Codable, you can deserialize JSON with JSONDecoder without third-party libraries. This tool generates Swift struct definitions with proper types (String, Int, Double, Bool), optional handling (String?), and CodingKeys enums.
The converter follows Swift conventions: PascalCase struct names, camelCase properties, [Element] for arrays, and Optional<T> as T? for nullable fields. When JSON keys use snake_case, it generates a CodingKeys enum for mapping. The output works with JSONDecoder in iOS, macOS, watchOS, and tvOS.
{
"user_name": "Alice",
"age": 30,
"roles": ["admin"],
"bio": null
}
struct Root: Codable {
let userName: String
let age: Int
let roles: [String]
let bio: String?
enum CodingKeys: String,
CodingKey {
case userName = "user_name"
case age, roles, bio
}
}
Get the most out of this tool
- Use JSONDecoder().keyDecodingStrategy = .convertFromSnakeCase instead of CodingKeys when all keys are snake_case.
- Mark fields as optional (String?) when the API may return null — non-optional fields cause decoding failure if missing.
- Swift structs with Codable work with both JSONDecoder and JSONEncoder — round-trip JSON without extra code.
Ready to generate your Swift structs?
Free forever. No signup. Instant results.
Open JSON to Swift ConverterSwift structs and Codable for JSON
Swift's Codable protocol (introduced in Swift 4) provides a built-in, type-safe JSON serialization and deserialization mechanism. Any Swift type that conforms to Codable (which combines Encodable and Decodable) can be serialized to JSON with JSONEncoder and deserialized from JSON with JSONDecoder. The converter generates Swift structs conforming to Codable, which is the idiomatic modern Swift approach to JSON handling — no third-party libraries required.
Swift structs are preferred over classes for JSON data models for several reasons. Structs are value types — they are copied rather than referenced, which prevents shared mutable state bugs. Structs are automatically synthesized with memberwise initializers. With Codable, a Swift struct requires only the Codable conformance declaration and property definitions — the compiler synthesizes the entire encoding and decoding logic automatically when all properties are themselves Codable.
The converter maps JSON types to their Swift equivalents: strings become String, integers become Int, floating-point numbers become Double, booleans become Bool, null values become Optional types (String?), arrays become [ElementType], and nested objects become nested struct types. The property names are converted from snake_case (common in JSON APIs) to camelCase (Swift convention) using CodingKeys enum or the JSONDecoder's keyDecodingStrategy.
CodingKeys is the Swift mechanism for mapping between JSON key names and Swift property names. When a JSON field is "first_name" but the Swift property should be firstName, you add a CodingKeys enum to the struct that maps between the two. The converter generates these CodingKeys automatically when JSON keys use snake_case. Alternatively, setting JSONDecoder().keyDecodingStrategy = .convertFromSnakeCase handles the conversion globally without explicit CodingKeys.
Optional properties in Swift JSON models deserve careful thought. If a JSON field might be absent in some responses, mark its type as Optional (e.g., String?). JSONDecoder treats absent keys as nil for optional properties. For non-optional properties, an absent key causes a DecodingError.keyNotFound error at runtime. Getting optionality right prevents hard-to-debug crashes in production iOS apps when an API returns fewer fields than expected.
SwiftUI and Combine make JSON decoding even more streamlined. URLSession's dataTaskPublisher in Combine can be chained with .decode(type:decoder:) to produce a publisher that emits your decoded Swift struct. Async/await in Swift 5.5+ enables data(from:delegate:) returning (Data, URLResponse), then JSONDecoder().decode(YourType.self, from: data). The generated structs from this tool plug directly into both patterns.
When developers use this tool
Additional frequently asked questions
Should I use struct or class for JSON models in Swift?
Use struct for JSON models in almost all cases. Structs are value types with copy semantics, automatically preventing shared mutable state bugs. Classes are needed when you need inheritance or when the object must be shared by reference. For simple data transfer objects from APIs, struct is always the better choice.
How do I handle JSON date strings with Swift's JSONDecoder?
Set JSONDecoder().dateDecodingStrategy before calling decode(). For ISO 8601 dates, use .iso8601. For custom formats, use .formatted(DateFormatter) with your custom formatter. For Unix timestamps, use .secondsSince1970 or .millisecondsSince1970. In your Swift struct, use Date as the property type instead of String.
What happens if the JSON has extra fields my struct does not declare?
By default, JSONDecoder ignores JSON keys that do not have corresponding properties in your Codable struct. This is the lenient behavior. If you want strict decoding that rejects unknown keys, you can implement a custom init(from decoder: Decoder) that exhausts all coding keys and throws on unknown ones.
Can the generated Swift structs be used with Alamofire?
Yes. Alamofire's responseDecodable(of:) method accepts any Decodable type. Since the generated structs conform to Codable (which includes Decodable), you can use them directly: AF.request(url).responseDecodable(of: MyStruct.self) { response in ... }. No additional configuration is required.