JSON to Swift

Generate Swift Structs
from JSON

Paste any JSON and instantly get Swift struct definitions conforming to Codable. Perfect for iOS, macOS, and Swift API clients.

Open JSON to Swift Converter

Swift struct definitions in four steps

Paste your JSON Drop any valid JSON object or array into the input — as compact or pretty-printed as you like.
Select Swift output Switch to the Convert tab and choose Swift. Codable struct definitions are generated immediately.
Copy the struct definitions One-click copy puts the generated Swift code on your clipboard, ready to paste into your project.
Add to your Xcode project Paste the structs into your Swift file and use JSONDecoder to deserialize API responses immediately.

No account. No upload. No nonsense.

🔒

No Server

Your JSON never leaves your device. There is no backend to send it to.

iOS Ready

Generated structs conform to Codable and work with JSONDecoder out of the box for Swift 5.0+.

📡

Works Offline

Load once and use forever — even on a plane or without internet access.

Related JSON tools

JSON to TypeScript Generate typed TypeScript interfaces from your JSON structure instantly.
JSON Formatter Pretty-print JSON with syntax highlighting and a collapsible tree view.
JSON Schema Validator Validate JSON against a Draft-07 schema with detailed error messages.
JWT Decoder Decode and inspect JWT tokens — header, payload, and expiry status.

Common questions answered

Is JSON to Swift conversion free?

Yes, completely free with no signup required.

What Swift version is supported?

The output works with Swift 5.0+ and conforms to both Encodable and Decodable via the Codable typealias.

Does my data leave my browser?

No. All conversion is 100% client-side.

Are CodingKeys generated?

CodingKeys are generated when JSON keys contain characters not valid in Swift identifiers (e.g. hyphens, spaces).

How are optional values handled?

Fields that may be absent or null become Optional types (e.g., String? or Int?).

Converting JSON to Swift Codable structs

Swift Codable protocol provides built-in JSON encoding and decoding. By defining a struct conforming to Codable, you can deserialize JSON with JSONDecoder without third-party libraries. This tool generates Swift struct definitions with proper types (String, Int, Double, Bool), optional handling (String?), and CodingKeys enums.

The converter follows Swift conventions: PascalCase struct names, camelCase properties, [Element] for arrays, and Optional<T> as T? for nullable fields. When JSON keys use snake_case, it generates a CodingKeys enum for mapping. The output works with JSONDecoder in iOS, macOS, watchOS, and tvOS.

Convert JSON to Swift structs
Input
{
  "user_name": "Alice",
  "age": 30,
  "roles": ["admin"],
  "bio": null
}
Output
struct Root: Codable {
    let userName: String
    let age: Int
    let roles: [String]
    let bio: String?

    enum CodingKeys: String,
      CodingKey {
        case userName = "user_name"
        case age, roles, bio
    }
}

Get the most out of this tool

Ready to generate your Swift structs?

Free forever. No signup. Instant results.

Open JSON to Swift Converter

Swift structs and Codable for JSON

Swift's Codable protocol (introduced in Swift 4) provides a built-in, type-safe JSON serialization and deserialization mechanism. Any Swift type that conforms to Codable (which combines Encodable and Decodable) can be serialized to JSON with JSONEncoder and deserialized from JSON with JSONDecoder. The converter generates Swift structs conforming to Codable, which is the idiomatic modern Swift approach to JSON handling — no third-party libraries required.

Swift structs are preferred over classes for JSON data models for several reasons. Structs are value types — they are copied rather than referenced, which prevents shared mutable state bugs. Structs are automatically synthesized with memberwise initializers. With Codable, a Swift struct requires only the Codable conformance declaration and property definitions — the compiler synthesizes the entire encoding and decoding logic automatically when all properties are themselves Codable.

The converter maps JSON types to their Swift equivalents: strings become String, integers become Int, floating-point numbers become Double, booleans become Bool, null values become Optional types (String?), arrays become [ElementType], and nested objects become nested struct types. The property names are converted from snake_case (common in JSON APIs) to camelCase (Swift convention) using CodingKeys enum or the JSONDecoder's keyDecodingStrategy.

CodingKeys is the Swift mechanism for mapping between JSON key names and Swift property names. When a JSON field is "first_name" but the Swift property should be firstName, you add a CodingKeys enum to the struct that maps between the two. The converter generates these CodingKeys automatically when JSON keys use snake_case. Alternatively, setting JSONDecoder().keyDecodingStrategy = .convertFromSnakeCase handles the conversion globally without explicit CodingKeys.

Optional properties in Swift JSON models deserve careful thought. If a JSON field might be absent in some responses, mark its type as Optional (e.g., String?). JSONDecoder treats absent keys as nil for optional properties. For non-optional properties, an absent key causes a DecodingError.keyNotFound error at runtime. Getting optionality right prevents hard-to-debug crashes in production iOS apps when an API returns fewer fields than expected.

SwiftUI and Combine make JSON decoding even more streamlined. URLSession's dataTaskPublisher in Combine can be chained with .decode(type:decoder:) to produce a publisher that emits your decoded Swift struct. Async/await in Swift 5.5+ enables data(from:delegate:) returning (Data, URLResponse), then JSONDecoder().decode(YourType.self, from: data). The generated structs from this tool plug directly into both patterns.

When developers use this tool

iOS app API integration Generate Swift structs from a third-party REST API's JSON documentation to use with URLSession and JSONDecoder. Get type-safe access to all response fields without writing boilerplate serialization code.
SwiftUI data modeling SwiftUI's data flow relies on value-type models. Generate the Swift struct from your API JSON and use it directly as @State, @StateObject, or ObservableObject published property in your SwiftUI view hierarchy.
Unit test fixture setup Convert a recorded API JSON response to a Swift struct definition and use it as a mock return value in XCTest unit tests, removing the need to parse JSON at test time and making tests faster and more reliable.
macOS app development Mac Catalyst and native macOS apps built with Swift use the same Codable protocol for JSON handling. Generated structs work unchanged across iOS, iPadOS, macOS, watchOS, and tvOS targets.

Additional frequently asked questions

Should I use struct or class for JSON models in Swift?

Use struct for JSON models in almost all cases. Structs are value types with copy semantics, automatically preventing shared mutable state bugs. Classes are needed when you need inheritance or when the object must be shared by reference. For simple data transfer objects from APIs, struct is always the better choice.

How do I handle JSON date strings with Swift's JSONDecoder?

Set JSONDecoder().dateDecodingStrategy before calling decode(). For ISO 8601 dates, use .iso8601. For custom formats, use .formatted(DateFormatter) with your custom formatter. For Unix timestamps, use .secondsSince1970 or .millisecondsSince1970. In your Swift struct, use Date as the property type instead of String.

What happens if the JSON has extra fields my struct does not declare?

By default, JSONDecoder ignores JSON keys that do not have corresponding properties in your Codable struct. This is the lenient behavior. If you want strict decoding that rejects unknown keys, you can implement a custom init(from decoder: Decoder) that exhausts all coding keys and throws on unknown ones.

Can the generated Swift structs be used with Alamofire?

Yes. Alamofire's responseDecodable(of:) method accepts any Decodable type. Since the generated structs conform to Codable (which includes Decodable), you can use them directly: AF.request(url).responseDecodable(of: MyStruct.self) { response in ... }. No additional configuration is required.