Skip to content
100% local

JSON to Swift Codable struct

Turn a JSON sample into a Swift struct that conforms to Codable.

Input
Output

JSON to Swift Codable struct

Paste a JSON sample and this tool infers its shape and generates a Swift struct that conforms to Codable, ready to drop into an iOS, macOS or server-side Swift project. Nested objects become their own nested structs, arrays become typed Swift arrays, and a value that is sometimes missing or null becomes an optional property with a question mark — so the generated model matches what the API actually sends, not just one lucky sample.

The options control the shape of the generated code. Choose struct or class for the declaration, and let or var for property mutability. Turning on CodingKeys converts snake_case JSON keys such as user_name into Swift's camelCase convention and adds the matching CodingKeys enum, so decoding still lines up with the original field names; leave it off to keep the JSON keys verbatim. Access level sets whether the type and its properties are public or internal, dates as Date recognises ISO-8601 strings and types them as Date instead of String, and indentation lets you match your project's style with 2 spaces, 4 spaces or tabs.

A field that mixes unrelated types across a sample array — a value that is sometimes a number and sometimes a string — falls back to a small AnyCodable wrapper that is only added to the output when it is actually needed, so the struct still compiles instead of silently guessing wrong. Keys that aren't valid Swift identifiers, including emoji or keys starting with a digit, are sanitized and mapped back through CodingKeys automatically so nothing is lost.

Everything runs locally in your browser as you type — no JSON sample is ever uploaded or sent to a server, which matters when the sample comes from a real API response. Copy the generated Swift, download it as a .txt file, or send it back into the input to refine it further.

FAQ

How are nested JSON objects handled?
Each nested object becomes its own Swift struct, named after the key it appeared under, and referenced from the parent struct by type.
What happens to a field that is sometimes null or missing?
It is generated as an optional property with a trailing question mark, so decoding never crashes on a real response that omits it.
What does the CodingKeys option actually do?
When it's on, a snake_case JSON key like created_at becomes the property createdAt, and a CodingKeys enum maps it back for decoding. When it's off, the property name matches the JSON key as closely as Swift's identifier rules allow.
Can it generate a class instead of a struct?
Yes. The Declaration option switches every generated type between struct and class while keeping the same Codable conformance.
Is my JSON uploaded anywhere?
No. The struct is generated entirely in your browser — your JSON sample never leaves your device.