JSON to Swift Struct

Infer Swift Codable structs from a JSON sample.

About JSON to Swift Struct

Generate Swift Codable structs from a JSON sample, with optionals for missing or null values and CodingKeys only where a key is renamed.

How to use

  1. Paste JSON, or load a .json file (up to 5 MB). The root must be an object or an array of objects.
  2. Optionally change the root struct name (default Root). It must start with a letter or underscore and contain only letters, digits and underscores.
  3. The Swift code updates as you type. Copy it, or Download it as a .swift file.

What it does

JSON Swift
string String
integer Int
other number Double
boolean Bool
array [T]
nested object a named struct, RootAddress
null in some items, or key missing from some items T?
only null, mixed types, or the items of an empty array JSONValue
  • Every struct conforms to Codable and has let properties in camelCase. Nested structs are listed before the struct that uses them.
  • CodingKeys is added only to a struct in which some key differs from its property name, as user_name differs from userName. It then lists every property, because Swift treats the enum as the complete list of coded keys.
  • Property names that are Swift keywords, such as class and init, are written in backticks. A key that starts with a digit gets an underscore in front.
  • Where the sample gives no single type, the property is typed JSONValue, and a small enum JSONValue: Codable that can hold any JSON value is added at the end of the output.
  • It does not use Date: date strings stay String, because decoding a date depends on the decoder's dateDecodingStrategy. It does not generate classes, var properties or hand-written decoding for your own structs.

Example

{ "id": 1, "user_name": "Ada", "tags": ["admin"] }
struct Root: Codable {
    let id: Int
    let userName: String
    let tags: [String]

    enum CodingKeys: String, CodingKey {
        case id
        case userName = "user_name"
        case tags
    }
}

Privacy

Inference runs in the page. No request is made and nothing is stored.

Frequently asked questions

What is Codable in Swift?
Codable is a type alias for Encodable and Decodable. A struct whose properties are all Codable gets its encoding and decoding code from the compiler, so JSONDecoder().decode(Root.self, from: data) works without hand-written parsing.
When do I need CodingKeys?
When a JSON key is not the property name you want, for example user_name for userName. Declare enum CodingKeys: String, CodingKey with one case per property and give each renamed case the JSON key as its raw value.
How do I decode snake_case JSON without CodingKeys?
Set decoder.keyDecodingStrategy = .convertFromSnakeCase on the JSONDecoder, which maps user_name to userName for every type it decodes. Remove the generated CodingKeys in that case, because the two do not combine.
What happens to an optional property when the JSON key is missing?
A property declared T? decodes to nil both when the key is absent and when its value is null. A non-optional property makes decoding throw keyNotFound or valueNotFound instead.