JSON to Swift

Generate Swift types from JSON automatically.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What JSON to Swift does

JSON to Swift turns a sample JSON document into Swift code: one Codable struct per object, with a type for every field inferred from the value in the sample.

JSON to Swift examples

The built-in sample

Input

{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}

Output

struct Address: Codable {
    let city: String
}

struct Root: Codable {
    let name: String
    let active: Bool
    let tags: [String]
    let address: Address
}

This is the input that the Load sample button fills in.

Awkward keys, a null, a decimal and arrays

Input

{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}

Output

struct Items: Codable {
    let sku: String
    let qty: Int
    let gift: Bool
}

struct Root: Codable {
    let order_id: Int
    let total: Double
    let paid: Bool
    let coupon: Any
    let items: [Items]
    let tags: [Any]
}

The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.

Each output was produced by running this tool on the input above it.

How it works

The sample is walked recursively and each value is mapped to a Swift type:

Sample valueSwift type
StringString
Whole numberInt
Decimal numberDouble
true / falseBool
null, or an empty array’s itemsAny
Array of T[T]
Objecta new named type

Using the generated code

The structs conform to Codable:

let root = try JSONDecoder().decode(Root.self, from: data)

Details and limits

  • A property typed Any does not compile in a Codable struct. The generator uses Any for null values and empty arrays; replace it with an optional of the real type, such as String?.
  • Decoding fails if a key is missing from the data. Declare such properties as optionals.
  • A renamed key (order-id becomes order_id) needs a CodingKeys enum that maps the property to the original key.
  • A type is named after the key that holds the object: items gives a type called Items, with no attempt to make it singular. The top-level type is always Root.
  • If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
  • Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
  • Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.

Questions

Is my data uploaded when I use JSON to Swift?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does JSON to Swift do?

JSON to Swift turns a sample JSON document into Swift code: one Codable struct per object, with a type for every field inferred from the value in the sample.

Are there any limits I should know about?

A property typed Any does not compile in a Codable struct. The generator uses Any for null values and empty arrays; replace it with an optional of the real type, such as String?. Decoding fails if a key is missing from the data. Declare such properties as optionals.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.