←Tools Home/

JSON to Swift Codable Struct Converter

Transform arbitrary JSON payloads into type-safe Swift Codable structs with custom CodingKeys mapping, optional properties for null values, and nested struct extraction.

Source JSON Payload
784 chars
Swift Codable models generated successfully.
Generated Swift Models
import Foundation


public struct UserResponse: Codable, Sendable {
    public let `id`: String
    public let firstName: String
    public let lastName: String
    public let emailAddress: String
    public let isActiveMember: Bool
    public let creditBalance: Double
    public let loginCount: Int
    public let avatarUrl: String?
    public let createdAt: String
    public let addressInfo: AddressInfo
    public let tagList: [String]
    public let securityRoles: [SecurityRole]
    public let preferences: Preferences

    public enum CodingKeys: String, CodingKey {
        case `id`
        case firstName = "first_name"
        case lastName = "last_name"
        case emailAddress = "email_address"
        case isActiveMember = "is_active_member"
        case creditBalance = "credit_balance"
        case loginCount = "login_count"
        case avatarUrl = "avatar_url"
        case createdAt = "created_at"
        case addressInfo = "address_info"
        case tagList = "tag_list"
        case securityRoles = "security_roles"
        case preferences
    }
}

public struct Preferences: Codable, Sendable {
    public let darkModeEnabled: Bool
    public let notificationFrequency: String
    public let backupIntervalDays: Int

    public enum CodingKeys: String, CodingKey {
        case darkModeEnabled = "dark_mode_enabled"
        case notificationFrequency = "notification_frequency"
        case backupIntervalDays = "backup_interval_days"
    }
}

public struct SecurityRole: Codable, Sendable {
    public let roleId: String
    public let displayLabel: String
    public let accessLevel: Int
    public let permissions: [String]

    public enum CodingKeys: String, CodingKey {
        case roleId = "role_id"
        case displayLabel = "display_label"
        case accessLevel = "access_level"
        case permissions
    }
}

public struct AddressInfo: Codable, Sendable {
    public let streetLine1: String
    public let postalCode: String
    public let cityName: String
    public let countryCode: String

    public enum CodingKeys: String, CodingKey {
        case streetLine1 = "street_line1"
        case postalCode = "postal_code"
        case cityName = "city_name"
        case countryCode = "country_code"
    }
}
Target: iOS 13+ / macOS 10.15+ / watchOS 6+Swift 5.9+ / Swift 6 Sendable
Structs Generated4
Properties Mapped24
JSON Payload Size24
Technical Reference & Architecture Guide

JSON to Swift Codable Struct Converter (iOS / macOS Model Generator) — Reference Guide

Transform arbitrary JSON payloads into type-safe Swift Codable structs with custom CodingKeys mapping, optional properties for null values, and nested struct extraction.

Key Parameters & Controls

rootStructNameString

The identifier used for the top-level Swift struct (e.g., 'UserResponse' or 'PayloadRoot').

keyDecodingStrategyEnum (useCamelCase | convertFromSnakeCase | customCodingKeys)

Determines how property names are mapped. 'customCodingKeys' generates explicit CodingKeys enum cases mapping snake_case keys to camelCase properties, while 'convertFromSnakeCase' assumes JSONDecoder().keyDecodingStrategy handles conversion at runtime.

accessLevelEnum (internal | public | open | fileprivate)

Access control modifier prepended to structs, initializers, and synthesized CodingKeys enums.

generateIdentifiableBoolean

When true, automatically conforms models containing an 'id' property (UUID, Int, or String) to the SwiftUI 'Identifiable' protocol.

nestedStructExtractionEnum (inline | separateFiles | flatNamespaced)

Specifies whether child objects are defined as inner types inside the parent struct, generated as independent top-level structs, or generated for separate file exports.

Pro Tips & Best Practices

Common Gotchas & Troubleshooting

Swift compiler errors out with 'Type does not conform to protocol Decodable' when encountering `[String: Any]` dictionary fields.

Swift's `Any` type cannot conform to `Decodable`. Replace heterogeneous dictionary types with a strongly typed generic wrapper like `AnyCodable` or define a dedicated struct/enum with associated values for valid schema variants.

Runtime error `DecodingError.keyNotFound` or `DecodingError.valueNotFound` thrown when parsing nullable or conditionally absent JSON attributes.

Declare all properties that can evaluate to `null` or may be omitted from specific backend responses as optional (`Type?`). The synthesized Codable initializer will automatically assign `nil` if the key is missing or explicitly null.

Compiler warns 'Ambiguous reference to member CodingKeys' when using nested struct extraction with identical child names.

Namespace child structs under their parent (e.g., `User.Address` instead of top-level `Address`) or supply unique struct name prefixes during generation to prevent symbol collisions across scopes.

Frequently Asked Questions

▼

▼

▼

▼

Related Tools & Utilities

All Tools →