JSON To Swift Generator

Need to convert Swift into JSON Schema?

Swift Struct → JSON Schema

JSON Input

Loading editor…

Generated Swift

Configuration

Enter JSON data to generate Swift structs

Structs with Codable support and type safety

How to convert JSON to Swift – step-by-step guide

Use this JSON to Swift generator to create Codable Swift structs from JSON samples for iOS/macOS apps and Swift services.

  1. Step 1 – Paste a JSON sample

    • Paste a representative JSON object or array into the left editor.
    • Include nested objects, arrays, and nullable fields so types are inferred correctly.
    • Use Import to load JSON from a file, URL, or sample data.
  2. Step 2 – Choose Swift struct options

    • Set a root Struct Name (for example Root).
    • Review how optional fields map to String? and similar types.
    • Confirm key mapping strategy when JSON keys don’t match Swift naming conventions.
  3. Step 3 – Review the generated code

    • Check field names, types, and how arrays/objects are modeled.
    • Adjust options like Root Type Name, null handling, and frameworks if available.
    • If a field is inferred incorrectly, tweak your sample JSON and regenerate.
  4. Step 4 – Use the structs with Codable

    • Add the generated structs to your project and ensure they conform to Codable.
    • Decode JSON using JSONDecoder into the root type.
    • Add custom CodingKeys or date decoding strategies as needed.
  5. Step 5 – Copy or download

    • Copy the output into your project or download it as a file.
    • Run your formatter/linter to match your code style.
    • Add JSON parsing/serialization libraries if your language requires them.

Quick tips

  • Prefer ISO8601DateFormatter/JSONDecoder.dateDecodingStrategy for timestamps.
  • Use CodingKeys for snake_case to camelCase conversion when needed.
  • Keep DTO structs separate from UI models to avoid coupling.
Example output (simplified)
// JSON input
{
  "id": 123,
  "name": "Maeve Winters",
  "email": "maeve@example.com",
  "active": true,
  "roles": ["admin", "editor"],
  "metadata": { "plan": "pro" },
  "createdAt": "2024-03-01T10:15:00Z",
  "score": 99.5,
  "notes": null
}

// Generated Swift models (simplified)
struct Metadata: Codable {
  let plan: String
}

struct Root: Codable {
  let id: Int
  let name: String
  let email: String?
  let active: Bool
  let roles: [String]
  let metadata: Metadata
  let createdAt: String
  let score: Double
  let notes: String?
}

Related JSON & Swift tools

Explore more JSON and schema tools that work great alongside this JSON to Swift generator.

Frequently Asked Questions

What Swift features are supported?

The generator creates Swift structs with proper type safety, supports Codable protocol for JSON serialization/deserialization, uses optional types for null values, and follows Swift naming conventions.

How does Codable protocol work?

When Codable framework is selected, generated structs automatically conform to Codable protocol, enabling seamless JSON encoding/decoding with JSONEncoder and JSONDecoder.

How are optional values handled?

Null values in JSON are mapped to optional types (?) in Swift, providing type safety and requiring explicit unwrapping, following Swift's approach to null safety.

What about Swift's type system?

Generated Swift code leverages Swift's strong type system with appropriate types (Int, Double, String, Bool, [Any], [String: Any]) and value semantics using structs for immutable data models.

JSON to Swift Struct Generator Online | JSONSwiss