JSON to Swift Model
Paste JSON and get Swift models that conform to Codable, ready for JSONDecoder. Properties are camelCase, a CodingKeys enum maps any that differ from the JSON keys, fields missing or null in the sample become optionals, ISO dates can become Date, and values with mixed types use a small JSONValue enum that is generated when needed.
- Runs in your browser
- No sign-up
- Free to use
How to use JSON to Swift Model
- Paste a JSON sample.
- Choose struct or class and let or var.
- Set the root type name and date detection.
- Copy the code or download Models.swift.
JSON to Swift Model features
Codable
Decodable and Encodable with no extra code for matching keys.
CodingKeys
Generated only when a property name differs from its key.
Optionals
Type? for fields missing or null in the sample.
Dates
Date for ISO 8601 strings, with a decoder note.
Mixed values
A generated JSONValue enum for fields whose type varies.
Local
Generated in your browser.
When to use JSON to Swift Model
- Decoding a REST API response in an iOS or macOS app.
- Modelling a JSON configuration bundled with an app.
- Creating preview data for SwiftUI views.
- Building a Swift server model with Vapor.
JSON to Swift Model FAQ
How do I decode the JSON?
let model = try JSONDecoder().decode(Root.self, from: data). For arrays, decode [Item].self.
Why is there a CodingKeys enum?
Swift property names must be valid identifiers and follow camelCase. When a JSON key such as "is-active" differs, CodingKeys tells Codable which key belongs to which property.
How are dates handled?
With date detection on, ISO 8601 strings become Date. Set decoder.dateDecodingStrategy = .iso8601, or decoding fails.
Struct or class?
Structs are value types and the usual choice for models. Use a final class if you need reference semantics, for example with some persistence frameworks.
What is JSONValue?
An enum generated when a field’s type varies or is unknown. It decodes any JSON value and encodes it back unchanged.
Is my JSON uploaded?
No. Everything runs in your browser.
Codable models from JSON
Swift’s Codable protocol lets JSONDecoder turn JSON into typed structs with almost no code, provided the struct’s properties match the JSON. Property names, types and optionality all have to be right: a missing key decoded into a non-optional property throws an error, as does a number decoded into a String. Generating the structs from a real sample gets these details right from the start.
Each JSON object becomes a struct conforming to Codable, named after its key. Properties use Swift’s camelCase convention. When every property name equals its JSON key, no extra code is needed; otherwise a CodingKeys enum lists every property and maps the renamed ones to their keys, which is how Codable supports snake_case or hyphenated keys.
Optionals come from the sample. A field missing from some objects in an array, or null in any of them, becomes an optional, which JSONDecoder sets to nil when the key is absent. Non-optional properties make decoding fail loudly when data is missing, which is useful as long as the sample was representative.
Dates are a common stumbling block. JSON has no date type, and JSONDecoder’s default strategy expects a number of seconds since 2001. When ISO 8601 strings are detected and typed as Date, the generator reminds you to set the .iso8601 decoding strategy. Values whose type varies are typed with a generated JSONValue enum instead of failing.
Integers become Int and decimals Double, which covers most APIs. Review fields that represent money or very large identifiers, and consider enums for string fields with a known set of values; Codable supports both with small changes to the generated code.