jsondecode.com logo

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 BigQuery Schema Converter — Free Online ToolJSON to React Flow Diagram — Convert JSON Online FreeJSON to Go Struct with BSON Tags — Free Online ToolJSON to TypeScript Interface Generator — Free OnlineJSON to YAML Converter — Convert JSON to YAML OnlineJSON to CSV Converter — Export JSON Array to CSV FreeJSON to Python Dataclass Generator — Free Online ToolJSON to SQL INSERT Statement Generator — Free OnlineJSON to Markdown Table Converter — Free Online ToolJSON to XML Converter — Convert JSON to XML Online FreeJSON to HTML Table Converter — Free Online ToolYAML to JSON Converter — Convert YAML to JSON Online FreeXML to JSON Converter — Convert XML to JSON Online FreeJSON to JSON Schema Generator — Free Online ToolJSON to GraphQL Schema Generator — Free Online ToolRuby to JSON Converter — Convert Ruby Hashes to JSON OnlineJSON to C# Class Generator — Free Online ToolJSON to Java Class (POJO) Generator — Free Online ToolJSON to Kotlin Data Class Generator — Free Online ToolJSON to Rust Struct Generator — Free Online ToolJSON to PHP Array Converter — Free Online ToolCSV to JSON Converter — Free Online ToolJSON to Dart Class Generator — Free Online ToolJSON to Terraform HCL Variables Converter — Free OnlineJSON to Mongoose Schema Generator — Free Online ToolJSON to Prisma Schema Model Generator — Free OnlineJSON to Protocol Buffer (proto3) Generator — Free OnlineJSON to TOML Config Format Converter — Free Online ToolTOML to JSON Converter — Convert TOML to JSON Online FreeJSON to Apache Avro Schema Generator — Free Online ToolJSON to OpenAPI 3.0 Schema Component — Free Online ToolJSON to R Data Frame Code Generator — Free Online ToolJSON to Lua Table Syntax Converter — Free Online ToolJSON to Zod Schema (TypeScript) Generator — Free OnlineJSON to Scala Case Class with Circe Codec — Free OnlineJSON to PowerShell Hashtable Converter — Free Online Tool

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 TypeExample ValueSwift TypeNotes
String"hello"StringDirect mapping; optional becomes String?
Number (integer)42IntUse Int64 for large integers beyond 32-bit range
Number (float)3.14DoubleFloat also valid; Double preferred for precision
BooleantrueBoolMaps directly; no ambiguity
NullnullOptional (e.g. String?)Any JSON field that can be null becomes an optional
Object{"key": "val"}Nested structGenerator 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] / AnyRequires a custom AnyCodable wrapper; avoid in practice

Codable vs Alternative Serialization Approaches in Swift

ApproachLibraryRequires Manual MappingSupports Nested TypesSwift 5.9+ Macro SupportTypical Use Case
Codable (struct)Foundation (stdlib)No (auto-synthesized)YesNo (manual)REST APIs, Plist, UserDefaults
Codable (class)Foundation (stdlib)NoYesNoReference-type models, inheritance
ObjectMapperObjectMapper (3rd party)YesYesNoLegacy codebases, custom transforms
SwiftyJSONSwiftyJSON (3rd party)Yes (subscript access)Manual traversalNoQuick prototyping, dynamic JSON
@Observable + CodableObservation (Swift 5.9)NoYesYesSwiftUI state + network models
Manual JSONSerializationFoundation (stdlib)Yes (full boilerplate)ManualNoPerformance-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.