JSON to Swift
Paste JSON and instantly get Codable Swift struct definitions with inferred types for every field.
Example
Given this JSON:
{
"id": 1,
"name": "Alice",
"tags": ["swift", "ios"],
"address": { "city": "NYC" }
}You get:
struct Root: Codable {
let id: Int
let name: String
let tags: [String]
let address: Address
}
struct Address: Codable {
let city: String
}How it works
It parses your JSON, infers Swift types (Int, Double, Bool, String, nested structs, arrays), and emits Codable structs starting with a root type named Root. Objects appearing in arrays are merged so keys missing from some elements become optionals.
Good to know
JSON to Swift turns a sample JSON payload into ready-to-use Codable struct definitions, inferring the Swift type for every field so you don't have to hand-write the model layer. It's aimed at iOS, macOS, and server-side Swift developers who are wiring up a networking layer and want a fast, accurate starting point for decoding an API response. Paste a representative response, click Convert, and you get a top-level Root struct plus separate named structs for each nested object.
Reach for it whenever you're integrating a new endpoint, mocking out a response shape, or migrating data models from another language. Because the structs conform to Codable, you can decode straight away with JSONDecoder().decode(Root.self, from: data)—no manual CodingKeys or custom init needed for the common cases.
Read the output top-down: the first struct is always Root, and nested objects become their own structs named after their key (for example an address object becomes an Address struct). Pay attention to the chosen types—numbers without a decimal become Int while numbers with one become Double—and to any property ending in ?, which marks a field that was either null or absent from some array elements. The status line under the panes also tells you how many structs were generated.
- Feed it the richest sample you have: if a field is sometimes missing or null, include both cases so the tool can mark it optional correctly.
- Double-check inferred numeric types—a value like
9.5 becomes Double, but an ID that just happened to be whole in your sample will be typed Int and may need widening to Double or String for real data.
Frequently asked questions
How are optional fields decided?
When the same array contains objects with differing keys, any key missing from at least one element becomes optional (e.g. let nickname: String?). JSON null values are also typed as optional.
Why is the root type called Root, and what about a top-level array?
The outermost type is always named Root for predictability. If your JSON is a top-level array, Root wraps it as a single items property whose element type is generated from the merged array objects.
Is my data uploaded anywhere?
No — it runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.
People also ask
How do I decode the generated Swift structs from API data?
Use Swift's built-in JSONDecoder, for example: let model = try JSONDecoder().decode(Root.self, from: data). The generated structs already conform to Codable, so no extra setup is needed for standard fields.
Does the tool generate CodingKeys for snake_case JSON keys?
It does not emit explicit CodingKeys; it converts keys to camelCase Swift property names. For snake_case JSON, set decoder.keyDecodingStrategy = .convertFromSnakeCase, or add your own CodingKeys enum if the mapping is non-standard.
What's the difference between Codable, Decodable, and Encodable in Swift?
Decodable lets a type be created from external data (like JSON), Encodable lets a type be written out to data, and Codable is simply a typealias for both combined. The generated structs use Codable so they can be both decoded and encoded.
Will the generated structs handle dates from JSON?
Dates in JSON usually arrive as strings or numbers, so they are typed as String or Double here rather than Date. To get Date values, change the property type to Date and configure JSONDecoder's dateDecodingStrategy to match your format (for example .iso8601).
How should I handle JSON numbers that don't fit Int?
The tool infers Int for whole numbers and Double for decimals based on the sample, but very large integers or values that may exceed Int's range can be changed to Int64, Double, or String manually. Picking the type from a single sample can mislead, so verify against the API's documented ranges.
Can I convert JSON to a Swift class instead of a struct?
This tool outputs structs, which are value types and the common choice for Codable models. If you need reference semantics or inheritance, change the struct keyword to class and add an initializer, since classes require explicit init for stored properties.
Why are some properties marked with a question mark in the output?
A trailing question mark means the property is an optional, used when a field was null or was absent from at least one object in an array. Optionals decode successfully even when the key is missing from the JSON.
Are there other languages I can convert JSON to besides Swift?
Yes, CalcCafe offers sibling converters for Kotlin, Dart, Ruby, Objective-C, C++, and Haskell. They follow the same client-side, type-inference approach for their respective languages.
Related tools