JSON to Swift
This JSON to Swift tool generates structs that conform to Codable. CodingKeys handle snake_case mapping automatically, and optional types are inferred. Choose struct or class output. Generation runs locally in your browser, ready for iOS and macOS development.
How to use it
- 1
Paste your JSON
Paste API JSON; nested objects become separate structs and arrays become [T].
- 2
Confirm the root type name
The default is User; change it to match your domain model. Nullable fields are marked as optional properties.
- 3
Copy the Swift code
Codable structs with CodingKeys are generated, parsed with a single JSONDecoder call.
- 4
Decode the response
Run JSONDecoder().decode(User.self, from: data) to parse the payload, including nested models.
Key features
Codable with no dependencies
Based on Foundation Decodable/Encodable, so no third-party JSON library is required.
Automatic CodingKeys
Keys such as snake_case are mapped to Swift-style properties through CodingKeys, so a decoding-strategy mismatch is not a problem.
Smart optional marking
Null fields become optional types (String?), so a missing key does not fail decoding.
Struct or class
Value-type struct by default, with a class output style (including a final option) when reference semantics are needed.
Nested models decoded
Nested structs and arrays are generated together so one decode call handles the whole tree.
Local and private
Generation runs in your browser and the data is not uploaded.
Common errors & troubleshooting
- ! Prefer struct: it is a value type, thread-safe and performant, and fits SwiftUI data models well.
- ! A decoder can set keyDecodingStrategy = .convertFromSnakeCase, but when fields are irregular rely on the generated CodingKeys.
- ! A date represented as a string becomes String; once the format is confirmed, switch it to Date and configure decoder.dateDecodingStrategy.
- ! If the API guarantees a field is always present, remove the question mark to make it non-optional.
Frequently asked questions
How do I parse JSON with the generated code?
Let the struct conform to Codable (already generated), then call JSONDecoder().decode(User.self, from: data); nested models are decoded along with it.
Why are some properties optional (?)?
The field was null in the sample or may be missing. An optional property decodes to nil instead of throwing; if the API guarantees it is present, remove the question mark by hand.
Is Alamofire supported?
The generated content is independent of any networking library, so Alamofire responseDecodable can consume the Codable models directly; untick "Types only" for companion examples where a version provides them.
Is my data uploaded?
No. Generation runs locally in your browser.
Related tools
JSON to Kotlin
Generate Kotlin data classes from JSON. Supports Jackson, kotlinx.serialization and Klaxon, with a custom package name and accurate nullable (?) type annotations. Runs locally.
JSON to Dart
Generate Dart model classes for Flutter projects from JSON, including fields, a constructor and fromJson / toJson methods, with accurate nullable types and json_serializable or Freezed styles.
JSON to Rust
Generate Rust structs from JSON with serde derives. Optional fields are wrapped in Option<T>, and nested objects and Vec<T> are expanded. Runs locally in your browser.
JSON Formatter
Format, beautify and validate JSON in your browser. Errors point to the exact line and column, and you can indent with 2 spaces, 4 spaces or a tab.