JSON to Swift Codable Generator
Last updated:
Jsonic's JSON to Swift Codable generator converts a JSON object into Swift struct definitions conforming to Codable. Properties use let for immutability. JSON strings map to String, integers to Int, decimals to Double, booleans to Bool, and null values to optional types (String?, Int?). Arrays become [T] with the appropriate element type. A CodingKeys enum is generated when JSON keys differ from the camelCase property names. Nested objects generate separate named structs declared before the root struct. All processing runs in your browser with no data upload.
How to convert JSON to Swift Codable structs
- Paste your JSON object into the left panel, or click Example to load a sample.
- Optionally change the root struct name (default: Root).
- Click Generate.
- Copy the generated Swift structs into your Xcode project.
- Decode with: let obj = try JSONDecoder().decode(Root.self, from: data).
FAQ
What is Codable in Swift?
Codable is a type alias for the Encodable & Decodable protocols. Structs and classes conforming to Codable can be serialized and deserialized automatically using JSONEncoder and JSONDecoder. Swift synthesizes the implementation for you as long as all stored properties are also Codable.
How do I decode JSON with the generated struct?
Use JSONDecoder().decode(Root.self, from: data) where data is of type Data. If you have a JSON string instead of Data, convert it first with string.data(using: .utf8). For example: if let data = jsonString.data(using: .utf8) { let obj = try JSONDecoder().decode(Root.self, from: data) }.
When is CodingKeys generated?
CodingKeys is generated only when at least one JSON key differs from the camelCase Swift property name. For example, user_name becomes userName in Swift, which differs from the original key, so CodingKeys is added. If all JSON keys already match the camelCase property names exactly, CodingKeys is omitted to keep the output clean.
How are optional types handled?
JSON null values become optional Swift types such as String? or Int?. JSONDecoder handles missing keys by setting them to nil automatically if the property is declared optional. This means you can also safely decode JSON objects where that key is absent entirely.
How are nested objects handled?
Each nested JSON object becomes a separate Swift struct. Nested structs are declared before the parent struct in the output, which is the standard Swift source ordering. The parent struct references the nested type by name, for example let address: Address.
Can I use class instead of struct?
Yes. Replace struct with class in the generated output. Classes require an explicit initializer for Decodable synthesis, so you may need to add init(from decoder: Decoder) manually. Structs are generally preferred for JSON model types because they provide value semantics and are safer in concurrent code.
How do I encode a Swift struct back to JSON?
Use JSONEncoder().encode(value) which returns Data. To get a formatted JSON string, set encoder.outputFormatting = .prettyPrinted before encoding, then convert Data to String with String(data: encoded, encoding: .utf8).
How does this work with URLSession and async/await?
Use URLSession.shared.data(from: url) to get (Data, URLResponse). Then decode the Data with JSONDecoder. For example: let (data, _) = try await URLSession.shared.data(from: url); let result = try JSONDecoder().decode(Root.self, from: data). Use async let for parallel fetches. Swift Concurrency combined with Codable is the standard pattern for iOS and macOS networking.