JSON to Swift Struct

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.

Options on this page

Output formats: Struct (Codable struct with value semantics), Class (Final Codable class with an initializer).

OptionWhat it does
Root Type NameNames the top-level Struct.
Field Naming ConventionOne of camelCase, snake_case, PascalCase, Preserve Original. Defaults to camelCase.
Type for null valuesEither JSONValue or String, used when a key is null in every sample.
Separate TypesWrite 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 keysAdd 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.

How JSON values map to Swift types

JSON valueSwift type
stringString
integerInt
integer beyond 32 bitsInt64
decimalDouble
true / falseBool
ISO-8601 timestampString
array[T]
objectnested type
null or missing in some samplesT?
nullJSONValue (configurable)

Example

Input JSON

{
  "id": 1,
  "product_name": "Wireless Mouse",
  "tags": ["electronics"]
}

Generated Swift

struct Product: Codable {
    var id: Int
    var productName: String
    var tags: [String]

    enum CodingKeys: String, CodingKey {
        case id
        case productName = "product_name"
        case tags
    }
}

Frequently asked questions

How do I convert JSON to a Swift struct?

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.

Should I choose Struct or Class?

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.

Why does a struct with optional properties have its own encode(to:)?

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.

What is JSONValue?

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.

What happens to keys that are Swift keywords?

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.

Why are timestamps a String?

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.

Is my JSON uploaded anywhere?

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.

Is it safe to paste confidential JSON, such as API responses or customer data?

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.

Does it cost anything?

No. The generator is free to use with no sign-up and no usage limits.

How are nested objects handled?

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.

Other languages

More JSON tools