JSON to Swift Codable Struct Generator

Paste JSON and get Swift structs that conform to Codable, with nested types, arrays, optionals and CodingKeys for snake_case keys. Runs in your browser, so your data is never uploaded.

JSON Input

Ln 1, Col 1Empty

Swift

Your result will appear here.
No result yet

What Is a JSON to Swift Converter?

iOS and macOS apps read JSON with Swift's Codable protocol: you describe the data as structs, and JSONDecoder fills them in. A JSON to Swift converter writes those structs for you from an example response, with the right types, optionals for values that can be missing and CodingKeys wherever a JSON key is not a valid Swift name.

How to Convert JSON to Swift Structs

  1. Paste a sample of your JSON, or drop a .json file on the box.
  2. Choose a struct name, and let or var properties.
  3. Click Generate Swift, or press Ctrl + Enter.
  4. Copy the code, or download it as a .swift file.

Example

JSON
{
  "city": "London",
  "temp_c": 11.5,
  "feels_like_c": 9,
  "is_day": true,
  "wind": {
    "speed_kph": 18.4,
    "direction": "SW"
  },
  "hourly": [
    { "time": "09:00", "temp_c": 10 },
    { "time": "10:00", "temp_c": 11.2, "rain_mm": 0.4 }
  ]
}
Swift
import Foundation

struct Weather: Codable {
    let city: String
    let tempC: Double
    let feelsLikeC: Int
    let isDay: Bool
    let wind: Wind
    let hourly: [HourlyItem]

    enum CodingKeys: String, CodingKey {
        case city
        case tempC = "temp_c"
        case feelsLikeC = "feels_like_c"
        case isDay = "is_day"
        case wind
        case hourly
    }
}

struct Wind: Codable {
    let speedKph: Double
    let direction: String

    enum CodingKeys: String, CodingKey {
        case speedKph = "speed_kph"
        case direction
    }
}

struct HourlyItem: Codable {
    let time: String
    let tempC: Double
    let rainMm: Double?

    enum CodingKeys: String, CodingKey {
        case time
        case tempC = "temp_c"
        case rainMm = "rain_mm"
    }
}

temp_c becomes tempC, and CodingKeys maps it back to the JSON key. In hourly, temp_c is 10 in one item and 11.2 in the other, so it is a Double. rain_mm is Double? because only one hour has it. feels_like_c is an Int because the sample only shows a whole number: see the tips if it can have decimals.

How the Types Are Chosen

JSON valueSwift type
TextString
Whole numberInt (64-bit on every current Apple device)
Number with decimalsDouble
true / falseBool
ObjectIts own struct, like Wind
Array[T], like [HourlyItem]
Missing in some items, or nullAn optional, like Double?
Only null, or mixed typesJSONValue?, a small Codable enum the converter adds to the code. Replace it with the real type if you know it.

Two details are handled for you. Struct names that would clash with Swift or Foundation types get a suffix, so a "data" object becomes DataModel instead of hiding Foundation's Data. Keys that are Swift keywords, like default, are written in backticks.

Decode JSON in Swift

JSONDecoder with URLSession
import Foundation

func fetchWeather() async throws -> Weather {
    let url = URL(string: "https://api.example.com/weather?city=London")!
    let (data, _) = try await URLSession.shared.data(from: url)
    return try JSONDecoder().decode(Weather.self, from: data)
}

For a JSON string you already have, use Data(text.utf8) as the data. If the JSON is an array at the top level, decode the array type, for example [HourlyItem].self. To turn a struct back into JSON, use JSONEncoder().encode(weather).

Prefer no CodingKeys? Set decoder.keyDecodingStrategy = .convertFromSnakeCaseand delete the enums. That works for plain snake_case keys like temp_c, but not for keys with dashes or other characters, so the converter keeps the explicit keys.

Tips

  • Numbers that are sometimes decimals: if a field is 9 in your sample but can be 9.5 in real data, decoding it as Int fails. Change the type to Double.
  • Dates: date strings stay String. For ISO 8601 values like 2026-11-18T09:30:00Z, change the type to Date and set decoder.dateDecodingStrategy = .iso8601.
  • Read the error. When decoding fails, print the error: a DecodingError names the key and the type that did not match.
  • Check the JSON first. Broken JSON fails before any struct is filled. Fix it with JSON Repair or find the error in the JSON Validator.

Your JSON never leaves your browser, so it is safe to paste real API responses. Building for Android too? Try JSON to Kotlinor JSON to Dart for Flutter.

Frequently Asked Questions

How do I convert JSON to a Swift struct?

Paste a sample of your JSON into the converter and click Generate Swift. It writes Codable structs with nested types, arrays, optionals and CodingKeys that you can decode with JSONDecoder.

What are CodingKeys for?

CodingKeys map a Swift property name to a different JSON key, such as tempC to temp_c. The converter adds them whenever a key is not already a valid camelCase Swift name.

Why did my data object become DataModel?

A struct named Data would hide Foundation's Data type and break code that uses it, so names that clash with Swift or Foundation types get a Model suffix. You can rename it to anything else.

Is my JSON uploaded?

No. The Swift code is generated in your browser and your JSON never leaves your device.

Related Tools