JSON to Swift
Generate Codable Swift structs with CodingKeys from a JSON sample, using optionals and arrays. The parsing stays in your browser.
Swift · Swift 5.9 / Foundation. Parsing and type inference happen in this browser tab. Nothing is uploaded and no network request is made.
Is this tool not working?
Tell us what went wrong and we’ll fix it. The report is sent with the page you’re on (/json-to-swift/), so you don’t need to name the tool.
About the JSON to Swift
JSON to Swift turns a pasted JSON sample into Codable structs that can decode the same shape with Foundation’s JSONDecoder. The generator runs entirely in the browser, infers a Swift type at every position in the sample, and emits one struct per object with let properties. Property names are camelCase; whenever a JSON key differs from the property name — full_name versus fullName, a reserved keyword wrapped in backticks, or a name changed to avoid a collision — a nested CodingKeys enum records the exact original key. Arrays become Swift arrays such as [String] or [Project], nested objects become their own structs, and values that were null or missing from one of several merged objects become optionals. Numbers map to Double for the whole tree rather than being split into Int and Double, because a single value cannot establish whether a field is integral or what range it uses. Strings stay String and booleans stay Bool. When a value is null, or an array mixes types that cannot share one Swift type, the generated property uses a small JSONValue Codable enum included alongside the structs; it can decode strings, numbers, booleans, objects, arrays and null, so ambiguous samples still produce compiling code. A top-level array of objects generates the element struct and a comment noting the array root. Everything about this output is sample-based, and the limits matter. A key absent from your single example is assumed to be required; an empty array provides no element type; and nothing validates the JSON your app will receive at runtime. The tool does not infer Date from ISO strings, enum cases from a few values, polymorphic decoding, custom date strategies or any required-field rules. Swift keywords used as keys are emitted with backticks so the code stays valid, and duplicate type names receive numeric suffixes instead of silently merging two different shapes. Properties are declared with let, so a decoded model is immutable; add your own mutating helpers or copy methods if you need to edit it. Keys that camelCase to the same name get numbered properties while CodingKeys still records each original JSON spelling. No upload is involved: the JSON text is parsed locally and the generated source is only copied or downloaded by you. Use the structs as a quick starting point for an API model layer, then introduce dates, identifiers, enums and real validation after reading the API documentation. Treat the generated code as a draft that saves typing, not as proof that the remote service will always honour the shape you pasted.
How to use it
- Paste a JSON object or an array of objects into the JSON input box, or press Load example for a labelled sample.
- Set Root type name to the struct name for the top level; the default is Root and nested names come from your keys.
- Press Generate Swift, or press Ctrl/⌘ + Enter while the input is focused, to infer the types locally.
- Copy the structs or download the .swift file. CodingKeys enums appear only where a JSON key differs from its property.
- Add dates, enums and validation by hand where the API contract requires them before shipping the model.
Worked example
Good to know
This tool processes everything locally with JavaScript. Nothing you paste or upload is transmitted to Hellagape or to any third party, and closing the tab clears the data. There is no daily limit and no account requirement. Results are provided for information only; for legal, financial or medical decisions, confirm the outcome with a qualified professional.
FAQ
JSON to Swift questions
When is a CodingKeys enum generated?
Only when at least one property name differs from its JSON key. camelCase keys such as id need no enum. snake_case keys, backticked Swift keywords and renamed properties do. The enum lists every property, as Swift requires, and only differing cases carry a string value.
Why are numbers Double rather than Int?
A sample with 42 does not tell you whether the field is always an integer or how large it can get. Choosing Int can fail decoding on a later fractional or oversized value. Double is the safe neutral type; change it once the API contract is clear.
What is JSONValue in the output?
A small Codable enum included when a field is null-only or has conflicting sample types. It decodes any JSON value into one of its cases and encodes it back, so the struct still compiles and decodes. It is a fallback, not a typed model of the ambiguity.
Are optionals used for missing keys?
For keys that were null, and for keys missing from one of several merged array elements, yes. With a single object the tool cannot know that a key is optional, so it emits a non-optional property. JSONDecoder will throw if that key is later absent.
Does it handle dates, enums or polymorphism?
No. Every string stays String, and there is no inference of Date, enum cases or class hierarchies. Those need knowledge of the service and usually a custom decoder or CodingKeys strategy, so they are left for you to add.
Is the JSON uploaded or stored?
No. Parsing and code generation happen in your browser tab with no network request. The text exists only in the page until you copy or download the result.
Keep going