JSON to Swift
Paste a JSON sample. Get Swift types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Swift chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Swift:
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
// let root = try Root(json)
import Foundation
// MARK: - Root
struct Root {
let id: Int
let name: String
let active: Bool
let joined: Date
let tags: [String]
let address: Address
let scores: [Double]
}
// MARK: - Address
struct Address {
let city: String
let postcode: NSNull
}
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
// let root = try Root(json)
import Foundation
// MARK: - Root
struct Root: Codable {
let id: Int
let name: String
let active: Bool
let joined: Date
let tags: [String]
let address: Address
let scores: [Double]
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case active = "active"
case joined = "joined"
case tags = "tags"
case address = "address"
case scores = "scores"
}
}
// MARK: Root convenience initializers and mutators
extension Root {
init(data: Data) throws {
self = try newJSONDecoder().decode(Root.self, from: data)
}
init(_ json: String, using encoding: String.Encoding = .utf8) throws {
guard let data = json.data(using: encoding) else {
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
}
try self.init(data: data)
}
init(fromURL url: URL) throws {
try self.init(data: try Data(contentsOf: url))
}
and 127 more lines. Open the tool to see the whole file.
What you need in your project
Nothing. `Codable` is in the standard library.
The types are `Codable`, so `JSONDecoder` reads them with no other package.
"Struct or class" is the choice that matters most. A struct is copied by value and is the usual answer. A class is right when two parts of your program must see the same object.
Turn on "Sendable" for a type that crosses an actor boundary under Swift concurrency.
The trap to know about
A Swift decode fails on the whole document if one field is the wrong type. There is no partial read. Make a field optional when the API is not strict about it.
The options you can change
These belong to Swift, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Types only | Plain types only | false |
| Initialisers | Generate initializers and mutators | true |
| Explicit CodingKeys | Explicit CodingKey values in Codable types | true |
| Alamofire extensions | Alamofire extensions | false |
| Struct or class | Structs or classes | struct | class |
| final classes | Mark classes as final | false |
| var, not let | Use var instead of let for object properties | false |
| Objective-C support | Objects inherit from NSObject and @objcMembers is added to classes | false |
Questions
How do I turn JSON into Swift?
Paste your JSON into the box, choose Swift, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Swift project?
Nothing. `Codable` is in the standard library.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code