Convert JSON to Swift Codable structs or classes with CodingKeys
A free JSON to Swift converter: paste JSON and get Codable types back instantly. Choose a struct or a final class, both ready for JSONDecoder and JSONEncoder. Property types are inferred from the data (Int, Int64, Double, Bool, String, arrays and optionals), nested objects become their own types, and a CodingKeys enum maps camelCase properties to the original JSON keys. A null is written back as null, and values with no fixed type use a small JSONValue enum that is added to the output only when needed. Conversion runs entirely in your browser, so your JSON is never uploaded to a server.
Output formats: Struct (Codable struct with value semantics), Class (Final Codable class with an initializer).
| Option | What it does |
|---|---|
| Root Type Name | Names the top-level Struct. |
| Field Naming Convention | One of camelCase, snake_case, PascalCase, Preserve Original. Defaults to camelCase. |
| Type for null values | Either JSONValue or String, used when a key is null in every sample. |
| Separate Types | Write each type to its own .swift file. Swift needs no import to see another file of the same module. Off (the default) puts every type in one file, so Copy All pastes into a single file that compiles. |
| Map original JSON keys | Add a CodingKeys enum that maps each property to the original JSON key. Without it a property only binds to a key spelled exactly like it, so a snake_case key is not read into a camelCase property. |
| JSON value | Swift type |
|---|---|
| string | String |
| integer | Int |
| integer beyond 32 bits | Int64 |
| decimal | Double |
| true / false | Bool |
| ISO-8601 timestamp | String |
| array | [T] |
| object | nested type |
| null or missing in some samples | T? |
| null | JSONValue (configurable) |
{
"id": 1,
"product_name": "Wireless Mouse",
"tags": ["electronics"]
}
struct Product: Codable {
var id: Int
var productName: String
var tags: [String]
enum CodingKeys: String, CodingKey {
case id
case productName = "product_name"
case tags
}
}
Paste your JSON into the input pane and the Swift types appear beside it immediately. Set the root type name, pick Struct or Class, then copy the code or download it. Decode with JSONDecoder().decode(Root.self, from: data) and write it back with JSONEncoder().encode(root). Nested JSON objects become their own types and arrays are mapped element by element.
Struct is a value type and the usual choice for decoded JSON: copies are independent and you get a memberwise initializer for free. Class is a final reference type with an explicit initializer, for models that are shared and mutated in place. Both are Codable and produce the same JSON.
The Codable code Swift synthesizes leaves an optional that is nil out of the output, so a field that was null in your JSON would disappear when written back. The generated encode(to:) encodes the optional itself, which writes null. A struct with no optional properties uses the synthesized version.
When the data does not fix a property type, such as a value that is always null or a list that mixes numbers and strings, the property is a JSONValue: a small Codable enum that holds any JSON value. It is added to the output only when something uses it.
A property named class, default or in is written in backticks. A few names cannot be used even then (self, Self, Type, Protocol, init), so they get a trailing underscore. The JSON key is kept in the CodingKeys enum when Map original JSON keys is on.
Decoding into a Date needs a date strategy on the decoder and reformats the value when it is written back, so the document would no longer round-trip. A timestamp stays a String; convert it where you need a Date.
No. Parsing, type inference, and code generation all run in your browser using JavaScript. Your JSON is never sent to a remote server, and there is no account to create. The page keeps your last input and settings in your own browser (localStorage) so they survive a reload; nothing is stored anywhere else.
The conversion itself never transmits your JSON, so a confidential payload does not leave your machine. You can verify this: open your browser DevTools, switch to the Network tab, and convert something — no request carries your data. Two things to know: your last input is saved in your browser's localStorage (clear site data to remove it), and the Copy Link button embeds your JSON in the link, so anyone who opens that link, and the server that receives the request, can see it. Only share such links where you are comfortable with that.
No. The generator is free to use with no sign-up and no usage limits.
Each nested JSON object becomes its own named type, derived from the key it appeared under. Arrays of objects are widened across every element, so a field that is a whole number in one element and a fraction in another gets a type that holds both.