JSON to Swift (Codable)

Turn JSON into Swift Codable structs or classes, with CodingKeys for snake_case keys. Free JSON to Swift converter for iOS and macOS apps.

Runs in your browser. Your data never leaves your device.

How to use the JSON to Swift (Codable)

  1. Paste a sample JSON response.
  2. Choose struct or final class, the root type name and the options: all properties optional, var instead of let.
  3. Copy the code into a .swift file in Xcode.

About this tool

Paste JSON from your API and get Swift types that conform to Codable, ready for JSONDecoder. Nested objects become their own type, arrays become [Type], and when a JSON key is not a good Swift name (snake_case or dashes) the converter writes CodingKeys so the original key is kept. Choose struct or final class, make every property optional, and use var instead of let if you need to change values.

How to use JSON in Swift

Swift reads JSON with Codable: decode Data into your type with JSONDecoder, and encode it back with JSONEncoder. With URLSession and async/await it takes a few lines.

struct User: Codable {
    let id: Int
    let name: String
    let email: String?
}

let url = URL(string: "https://api.example.com/users/1")!
let (data, _) = try await URLSession.shared.data(from: url)
let user = try JSONDecoder().decode(User.self, from: data)

Sending JSON to an API

To pass JSON data in a POST request, encode your value, put it in httpBody and set the Content-Type header to application/json.

var request = URLRequest(url: URL(string: "https://api.example.com/users")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode(user)
let (data, response) = try await URLSession.shared.data(for: request)

Converting JSON to a string

JSONEncoder gives you Data. Turn it into a String with String(decoding: data, as: UTF8.self), and set encoder.outputFormatting = [.prettyPrinted, .sortedKeys] for readable output. In JavaScript the same job is JSON.stringify(value), and in Python json.dumps(value).

Swift, Kotlin, TypeScript and more

Building the same app for Android or the web? The JSON tools below make the matching Kotlin data class, TypeScript interface, C# class, Python dataclass and others from the same sample, so every client uses the same field names.

Frequently asked questions

What is JSON and why is it used?

JSON (JavaScript Object Notation) is a text format for data: objects in curly braces, lists in square brackets, and strings, numbers, true, false and null. Almost every web API uses it because it is small, easy to read, and every language can parse it.

How can I use JSON in Swift?

Make a Codable struct that matches the JSON (paste the JSON here to get it), then decode with JSONDecoder().decode(Type.self, from: data).

How do I pass JSON data in an API request?

Encode the value with JSONEncoder, set it as the request's httpBody, set Content-Type to application/json and send it with URLSession.

How can I convert JSON to a string?

In Swift, encode with JSONEncoder and use String(decoding: data, as: UTF8.self). In JavaScript use JSON.stringify, and in Python json.dumps.

Struct or class?

Use a struct for API models: value types are safer and Codable works the same. Choose a final class only when you need one shared object that several parts of the app change.

Is my JSON sent anywhere?

No. The code is made in your browser.

More json tools

JSON and code models

All 31 free tools →
JSON tool · Runs in your browser

JSON Formatter

Format JSON online: beautify messy JSON with 2 or 4 spaces or tabs, sort keys, minify it to one line and find errors. Free, runs in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON Validator

Check if JSON is valid and see the exact line and column of any error, with a hint on how to fix it. A free online JSON validator in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Kotlin Data Class

Paste JSON and get Kotlin data classes for Android, with kotlinx.serialization, Gson or Moshi names, nullable fields and defaults. Free, in your browser.

Free · No sign-upOpen tool
JSON tool · Runs in your browser

JSON to Dart (Flutter)

Convert JSON to null-safe Dart model classes for Flutter, with fromJson, toJson and an optional copyWith. Free, no sign-up, runs in your browser.

Free · No sign-upOpen tool