JSON to Swift Struct Converter

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

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.

Swift JSON with Codable

Swift's Codable protocol (combining Encodable + Decodable) enables type-safe JSON parsing with minimal boilerplate. It's the official Apple approach for JSON in Swift and iOS development.

JSON Input → Swift Codable Struct

// JSON
{"userId": 1, "name": "Alice", "isActive": true, "score": 98.5}

// Swift Struct
import Foundation

struct User: Codable {
    let userId: Int
    let name: String
    let isActive: Bool
    let score: Double

    enum CodingKeys: String, CodingKey {
        case userId = "userId"
        case name
        case isActive = "isActive"
        case score
    }
}

// Decode from JSON Data
let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let user = try decoder.decode(User.self, from: jsonData)
print(user.name) // Alice

// Encode to JSON
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let encoded = try encoder.encode(user)

JSONDecoder Configuration

OptionDescription
keyDecodingStrategy = .convertFromSnakeCasesnake_case JSON → camelCase Swift automatically
dateDecodingStrategy = .iso8601Parse ISO 8601 date strings to Date
nonConformingFloatDecodingStrategyHandle NaN/Infinity in JSON floats
outputFormatting = .prettyPrintedReadable JSON output when encoding

Swift Codable: Complete JSON Guide

This example shows the full workflow from JSON to generated Swift structs with decode and encode operations.

{
  "id": 1,
  "firstName": "Alice",
  "email": "alice@example.com",
  "address": {"city": "London", "postcode": "SW1A 1AA"},
  "scores": [95, 87, 92],
  "active": true
}
// Generated Swift structs
struct Address: Codable {
    let city: String
    let postcode: String
}

struct User: Codable {
    let id: Int
    let firstName: String
    let email: String
    let address: Address
    let scores: [Int]
    let active: Bool
}

// Decode from JSON
let decoder = JSONDecoder()
let user = try decoder.decode(User.self, from: jsonData)
print(user.firstName)  // "Alice"

// Encode back to JSON
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let outputData = try encoder.encode(user)
let jsonString = String(data: outputData, encoding: .utf8)!

CodingKeys: Custom JSON Field Names in Swift

// JSON uses snake_case, Swift uses camelCase
// Use CodingKeys to map them

struct User: Codable {
    let userId: Int
    let firstName: String
    let createdAt: Date

    enum CodingKeys: String, CodingKey {
        case userId = "user_id"
        case firstName = "first_name"
        case createdAt = "created_at"
    }
}

// Automatic snake_case conversion (simpler)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let user = try decoder.decode(User.self, from: jsonData)

// Also handle dates
decoder.dateDecodingStrategy = .iso8601

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