JSON to Swift Struct Converter

Paste any JSON and instantly generate Swift 5 structs with Codable conformance and CodingKeys. Free, private, no signup required.

Open JSON to Swift Converter →

Example: JSON to Swift Struct

Given this JSON from an API response:

{
  "user_id": 42,
  "full_name": "Jane Smith",
  "email": "jane@example.com",
  "is_active": true,
  "address": {
    "street": "123 Main St",
    "city": "San Francisco"
  }
}

The converter generates:

struct RootModel: Codable {
    let userId: Int
    let fullName: String
    let email: String
    let isActive: Bool
    let address: Address

    enum CodingKeys: String, CodingKey {
        case userId = "user_id"
        case fullName = "full_name"
        case email
        case isActive = "is_active"
        case address
    }
}

struct Address: Codable {
    let street: String
    let city: String
}

Swift JSON Type Mapping

JSON Type Swift Type Notes
stringStringDirect mapping
integer numberIntWhole numbers
decimal numberDoubleFloating point values
booleanBooltrue / false
nullType?Made optional
array[Type]Array of inferred type
objectNested structNamed after key

How to Use the Generated Swift Code

  1. Copy the generated structs into your Xcode project
  2. Use JSONDecoder to parse your API response data
  3. For snake_case APIs you can use decoder.keyDecodingStrategy = .convertFromSnakeCase to avoid CodingKeys entirely
  4. Mark optional fields with ? if they may be absent from the response
  5. For dates, set decoder.dateDecodingStrategy to match your API's date format
let decoder = JSONDecoder()
let user = try decoder.decode(RootModel.self, from: jsonData)

Frequently Asked Questions

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

Prefer struct for JSON models. Structs are value types, which means they are copied rather than referenced, making them safer for data models. They also work perfectly with Codable. Use class only when you need inheritance or reference semantics.

How do I handle optional fields in the API response?

If a JSON field may be absent or null, declare it as optional in Swift: let field: String?. JSONDecoder will set it to nil if the key is missing or the value is null. The converter marks fields with null values as optional automatically.

Can I use this for SwiftUI and iOS development?

Yes. The generated Codable structs work in any Swift environment: SwiftUI, UIKit, Vapor (server-side Swift), Swift Package Manager libraries, and macOS apps. The code requires Swift 4.1 or later.

What if my JSON has dynamic or unknown keys?

For JSON objects with dynamic keys, use [String: SomeType] in Swift instead of a struct. For mixed-type values, use [String: AnyCodable] with a third-party AnyCodable wrapper, or decode using a custom init(from:) implementation.

Convert your JSON to Swift now

Free, instant, 100% private. No account needed.

Open JSON to Swift Converter →

Also useful: JSON to TypeScript | JSON to Kotlin | JSON to Rust | JSON to Java | JWT Decoder