JSON to Swift Struct Converter
Turn a JSON sample into a Swift struct in your browser. Optional and nullable fields are kept apart, and nothing is uploaded.
What the JSON to Swift generator does
Each object becomes a Codable struct. A CodingKeys enum is emitted only when at least one property name differs from its JSON key, and it declares each case exactly once. A field whose JSON type varied between samples is typed JSONValue, a small Codable enum emitted once at the end of the file, because Swift's Any does not conform to Codable and code that does not compile is not a conversion.
How to use it
JSON to Swift Struct Converter: paste the JSON into the box above, set Root type name (default "Root"), and press Run to get the Swift struct below.
Worked example
Input:
{
"id": 42,
"displayName": "Ada Lovelace",
"active": true,
"score": 9.5,
"tags": [
"math",
"engine"
],
"address": {
"city": "London",
"country": "GB"
},
"projects": [
{
"name": "Analytical Engine",
"year": 1843
},
{
"name": "Note G",
"year": 1843,
"notes": "first published algorithm"
}
],
"nickname": null
}
Output:
// Generated from a JSON sample in the browser by AllTools — nothing was uploaded.
// Rename and adjust to taste; the shape is inferred from one example, not a schema.
import Foundation
struct Root: Codable {
let id: Int
let displayName: String
let active: Bool
let score: Double
let tags: [String]
let address: Address
let projects: [Project]
let nickname: JSONValue?
}
struct Address: Codable {
let city: String
let country: String
}
struct Project: Codable {
let name: String
let year: Int
let notes: String?
}
/// A field whose JSON type varied between samples. `Any` is not `Codable`;
/// this enum is, so the generated file compiles without a dependency.
enum JSONValue: Codable {
case string(String)
case number(Double)
case bool(Bool)
case null
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(String.self) { self = .string(value); return }
if let value = try? container.decode(Bool.self) { self = .bool(value); return }
if let value = try? container.decode(Double.self) { self = .number(value); return }
self = .null
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .string(let value): try container.encode(value)
case .number(let value): try container.encode(value)
case .bool(let value): try container.encode(value)
case .null: try container.encodeNil()
}
}
}
Why it runs in your browser
The conversion happens on this page with JavaScript. Your input is not posted to a server, so it works offline after the page loads and nothing is retained anywhere. The page reads JSON and returns Swift struct; reloading the page discards both.
Each object becomes a Codable struct. A CodingKeys enum is emitted only when at least one property name differs from its JSON key, and it declares each case exactly once. A field whose JSON type varied between samples is typed JSONValue, a small Codable enum emitted once at the end of the file, because Swift's Any does not conform to Codable and code that does not compile is not a conversion.
Turn a JSON sample into a Swift struct in your browser. Optional and nullable fields are kept apart, and nothing is uploaded.
How to use it
JSON to Swift Struct Converter: paste the JSON into the box above, set Root type name (default "Root"), and press Run to get the Swift struct below.
Why it runs in your browser
The conversion happens on this page with JavaScript. Your input is not posted to a server, so it works offline after the page loads and nothing is retained anywhere. The page reads JSON and returns Swift struct; reloading the page discards both.
What to Expect
Best for
- Converting JSON to Swift struct when the data is already on screen.
What this tool keeps
- Your input is not posted to a server, so it works offline after the page loads and nothing is retained anywhere.
Sample input
{ "id": 42, "displayName": "Ada Lovelace", "active": true, "score": 9.5, "tags": [ "math", "engine" ], "address": { "city": "London", "country": "GB" }, "projects": [ { "name": "Analytical Engine", "year": 1843 }, { "name": "Note G", "year": 1843, "notes": "first published algorithm" } ], "nickname": null }
Sample output
// Generated from a JSON sample in the browser by AllTools — nothing was uploaded. // Rename and adjust to taste; the shape is inferred from one example, not a schema. import Foundation struct Root: Codable { let id: Int let displayName: String let active: Bool let score: Double let tags: [String] let address: Address let projects: [Project] let nickname: JSONValue? } struct Address: Codable { let city: String let country: String } struct Project: Codable { let name: String let year: Int let notes: String? } /// A field whose JSON type varied between samples. `Any` is not `Codable`; /// this enum is, so the generated file compiles without a dependency. enum JSONValue: Codable { case string(String) case number(Double) case bool(Bool) case null init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() if let value = try? container.decode(String.self) { self = .string(value); return } if let value = try? container.decode(Bool.self) { self = .bool(value); return } if let value = try? container.decode(Double.self) { self = .number(value); return } self = .null } func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() switch self { case .string(let value): try container.encode(value) case .number(let value): try container.encode(value) case .bool(let value): try container.encode(value) case .null: try container.encodeNil() } } }
Frequently asked questions
How do I use the json to swift struct converter?
JSON to Swift Struct Converter: paste the JSON into the box above, set Root type name (default "Root"), and press Run to get the Swift struct below.
Is my input uploaded?
No. The text is parsed on this page with JavaScript and is never posted to a server.
Is there a size limit?
Yes: 5 MB per run. Larger input is refused with a message rather than freezing the tab.
Build custom tools for your store
Join the SDK program to create useful ecommerce utilities for your store.