JSON to Swift Struct (Codable) Generator — Free Online
Convert JSON to a Swift struct conforming to Codable with CodingKeys enum where field names differ from Swift conventions using AI. Free, no sign-up.
JSON to Swift Struct (Codable) Generator — Free Online
About JSON to Swift Struct (Codable) Generator — Free Online
This tool converts any JSON object into Swift structs that conform to the Codable protocol, automatically generating CodingKeys enums when property names differ from JSON keys. Developers use it to eliminate the tedious boilerplate of writing Decodable and Encodable conformances by hand, especially when working with REST APIs that return snake_case or camelCase JSON.
JSON to Swift Type Mapping
| JSON Type | Example Value | Swift Type | Notes |
|---|---|---|---|
| String | "hello" | String | Direct mapping; optional becomes String? |
| Number (integer) | 42 | Int | Use Int64 for large integers beyond 32-bit range |
| Number (float) | 3.14 | Double | Float also valid; Double preferred for precision |
| Boolean | true | Bool | Maps directly; no ambiguity |
| Null | null | Optional (e.g. String?) | Any JSON field that can be null becomes an optional |
| Object | {"key": "val"} | Nested struct | Generator creates a new Codable struct for each object |
| Array of strings | ["a","b"] | [String] | Element type is inferred from array contents |
| Array of objects | [{"id": 1}] | [NestedStruct] | Generates a separate struct for the object type |
| Mixed array | [1, "a"] | [AnyCodable] / Any | Requires a custom AnyCodable wrapper; avoid in practice |
Codable vs Alternative Serialization Approaches in Swift
| Approach | Library | Requires Manual Mapping | Supports Nested Types | Swift 5.9+ Macro Support | Typical Use Case |
|---|---|---|---|---|---|
| Codable (struct) | Foundation (stdlib) | No (auto-synthesized) | Yes | No (manual) | REST APIs, Plist, UserDefaults |
| Codable (class) | Foundation (stdlib) | No | Yes | No | Reference-type models, inheritance |
| ObjectMapper | ObjectMapper (3rd party) | Yes | Yes | No | Legacy codebases, custom transforms |
| SwiftyJSON | SwiftyJSON (3rd party) | Yes (subscript access) | Manual traversal | No | Quick prototyping, dynamic JSON |
| @Observable + Codable | Observation (Swift 5.9) | No | Yes | Yes | SwiftUI state + network models |
| Manual JSONSerialization | Foundation (stdlib) | Yes (full boilerplate) | Manual | No | Performance-critical or dynamic keys |
Frequently Asked Questions
How do I decode snake_case JSON keys into camelCase Swift properties?
Use JSONDecoder with keyDecodingStrategy set to .convertFromSnakeCase — this automatically maps snake_case JSON keys like user_first_name to camelCase Swift properties like userFirstName without needing a CodingKeys enum. If your naming is irregular, define a CodingKeys enum explicitly with the exact string values matching the JSON keys. The generator on this page emits CodingKeys whenever the JSON key does not already match Swift naming conventions.
Does the generated Swift struct support both encoding and decoding?
Yes — conforming to Codable is equivalent to conforming to both Encodable and Decodable. The generated structs can be used with JSONDecoder to parse incoming JSON and with JSONEncoder to serialize Swift values back to JSON. If you only need one direction, you can replace Codable with Decodable or Encodable to be more explicit.
How do I handle optional JSON fields in a Swift Codable struct?
Declare the property as an optional type (e.g., var email: String?) in the struct. When the key is missing from the JSON or its value is null, JSONDecoder will set the property to nil instead of throwing a DecodingError. If you want a non-optional property with a default value when the key is absent, implement init(from:) manually and use decodeIfPresent with the ?? operator.
What happens when a JSON array contains objects with different shapes?
Swift's type system requires arrays to have a uniform element type, so JSONDecoder cannot decode heterogeneous arrays into a typed [SomeStruct] directly. The common solutions are: define an enum with associated values conforming to Codable, use a custom init(from:) that tries multiple types in order, or fall back to [AnyCodable] using a community library like AnyCodable-FlightSchool. In practice, redesigning the API response to use a type discriminator field is the cleanest fix.
Can I use the generated structs with SwiftUI and @Observable or @State?
Yes — Codable structs integrate cleanly with SwiftUI. For local view state, pass a decoded struct directly to @State. For shared app state in Swift 5.9+, wrap the struct in an @Observable class or use @StateObject with an ObservableObject class that holds the decoded value. The struct itself does not need any modification; Codable and SwiftUI observation are orthogonal concerns.
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.