JSON Swift

JSON to Swift Converter (Free AI Tool)

Convert JSON to Swift Codable structs instantly for iOS and macOS development. Generate type-safe Swift models from REST API responses with automatic optional detection and nested struct creation. Eliminate manual model writing and ensure compile-time type safety.

Data Format Converter
Tools
JSON Input
Ready to convert
JSON Output
Converted output will appear here

Hint: Select conversion type, paste your data, and get instant conversion. Supports JSON, YAML, XML, Excel, PDF, and more.

Client-side only

How It Works

  1. 1

    Paste JSON API Response or Data Object

    Input your JSON from REST APIs (GET /users), GraphQL queries, or configuration files. Paste multiple response samples to improve optional property detection accuracy.

  2. 2

    AI Infers Swift Types and Generates Codable Structs

    The AI analyzes property types (Int, String, Double, Bool), detects null values to generate optionals (String?), creates nested structs for sub-objects, and adds Codable conformance for automatic JSONDecoder compatibility.

  3. 3

    Copy Production-Ready Swift Models

    Receive Swift structs with proper type annotations, optional handling (? and !), CodingKeys for JSON key mapping (snake_case to camelCase), and nested struct definitions. Ready for immediate use with JSONDecoder in Xcode.

JSON vs Swift Codable: Type Safety Comparison

Feature JSON (Runtime) Swift Codable (Compile-time)
Type Safety Runtime only Compile-time type checking
Null Handling null value Optional types (Type?)
Parsing Manual dictionary access Automatic Codable decoding
Validation Runtime checks needed Compiler enforced types
Refactoring Error-prone IDE-assisted, type-safe
API Contract Implicit, undocumented Explicit struct definition

Code Examples

Example 1: Simple API Response to Codable Struct

JSON Input
{
  "id": 123,
  "username": "johndoe",
  "email": "[email protected]",
  "isVerified": true,
  "lastLogin": null,
  "roles": ["admin", "user"]
}
Swift Output
struct User: Codable {
    let id: Int
    let username: String
    let email: String
    let isVerified: Bool
    let lastLogin: String?
    let roles: [String]
}

// Usage:
let decoder = JSONDecoder()
let user = try decoder.decode(User.self, from: jsonData)

Key Changes:

JSON keys convert to Swift struct properties with camelCase naming conventions. The converter infers Int, String, Bool types from JSON values. Null values (lastLogin) generate optional types (String?) to handle missing data safely. Arrays become Swift typed arrays [String]. The struct conforms to Codable protocol, enabling automatic JSON decoding with JSONDecoder. No manual dictionary casting or type checking is needed—the compiler enforces type safety.

Example 2: Nested Objects with CodingKeys

JSON Input
{
  "user_id": 456,
  "display_name": "Jane Smith",
  "profile": {
    "age": 30,
    "location": "San Francisco"
  },
  "preferences": {
    "theme": "dark",
    "notifications_enabled": true
  }
}
Swift Output
struct Profile: Codable {
    let age: Int
    let location: String
}

struct Preferences: Codable {
    let theme: String
    let notificationsEnabled: Bool
    
    enum CodingKeys: String, CodingKey {
        case theme
        case notificationsEnabled = "notifications_enabled"
    }
}

struct UserResponse: Codable {
    let userId: Int
    let displayName: String
    let profile: Profile
    let preferences: Preferences
    
    enum CodingKeys: String, CodingKey {
        case userId = "user_id"
        case displayName = "display_name"
        case profile, preferences
    }
}

Key Changes:

Nested JSON objects generate separate Swift structs (Profile, Preferences) that are referenced by type. Snake_case JSON keys (user_id, notifications_enabled) are mapped to Swift camelCase using CodingKeys enum. This allows JSON APIs with non-Swift naming to decode into idiomatic Swift code. Each nested level becomes a separate, reusable struct. The Codable protocol handles the entire hierarchy automatically, making complex API responses type-safe and easy to work with.

Frequently Asked Questions

How does the converter handle optional vs required properties?

The converter analyzes JSON samples to detect null values and missing keys, generating Swift optional types (String?) for nullable properties. If a property appears in all samples, it's marked as required (non-optional). For production use, validate against multiple API responses to ensure accurate optional inference. You can manually adjust property optionality based on API documentation.

Is my JSON API data stored during conversion?

No. All JSON to Swift conversion happens entirely client-side in your browser using JavaScript-based type inference. Your API responses—whether user data, financial records, or proprietary schemas—never leave your machine. The tool works offline once loaded.

Can the converter handle arrays of different object types?

Yes, but with limitations. Arrays with consistent object structures generate [ModelName] arrays. Mixed-type arrays (e.g., [{"type": "A"}, {"type": "B"}]) require manual refinement using Swift enums with associated values or protocol-based polymorphism. The converter generates a base type but complex polymorphic structures need custom Codable implementations.

Related Tools