Type generation

JSON to SwiftCodable models from real samples

Compare both records to see which properties become optional.

JSON to Swift

JSON input

Parsed and generated in this browser. Input is not uploaded or stored.

Generated source

Generation options

Int64 / Decimal; tested on Swift 6.3.1, macOS 26. Use makeJSONDecoder(); verify other deployment runtimes.

Root array samples define element Root; decode [Root].self.

2 structs · 2 optional properties

JSON to Swift turns API examples into a reviewable Swift Codable generator result. Include contrasting records: one complete response cannot reveal every field that may be absent.

This JSON to Swift struct draft includes nested models and original CodingKeys. It describes observed data; your application still defines validation, deployment compatibility and future API changes.

Why use JSON to Swift?

Merge more than the first record

JSON to Swift examines every object at the same array location. Missing keys and explicit null make a property optional, while consistently present values keep a concrete type.

Two sample object cards merge into one model, with a hollow slot representing an optional field.

Keep dates under your control

Timestamps remain strings by default. ISO inference connects only valid, complete timestamps to Date properties and a matching decoder helper; other strings remain unchanged.

A timestamp card branches to a preserved string and a clock through a selected conversion path.

Choose the model contract

01

Use varied object samples

The root must be an object or a nonempty array of objects. Root arrays generate an element model: decode [Root].self. Nested arrays merge all their elements, including later records with missing keys.

02

Review names and dates

Keep CodingKeys enabled for snake_case or unusual keys. Preserve-name mode retains valid original identifiers; camelCase mode normalizes them. ISO inference requires a timezone and at most three fractional digits.

03

Confirm replacements and export

Automatic generation waits 350 ms. Disable it for Generate or Ctrl/Cmd+Enter. Changes immediately disable stale exports. Example and Clear show a replacement preview with character counts; Apply saves one undo snapshot.

What the source contains

Structures that compile together

JSON to Swift emits var properties and nested helper structs in one file. Every model adopts Codable. Empty nested objects become dictionaries, and mixed values use a self-contained recursive JSONValue enum instead of Any.

Integers and decimal numbers

Integer-token groups inside signed 64-bit range use Int64. Decimal or exponent tokens, larger integers and numeric groups containing them use Foundation.Decimal. The browser reads original tokens without converting them through JavaScript Number.

Optional fields with familiar behavior

Observed mode marks absent or null properties optional; All permits omission everywhere. Swift synthesized Codable reads both missing and null as nil and normally omits nil properties when encoding. It does not preserve that distinction.

Explicit decoder setup

makeJSONDecoder() keeps the default key strategy and, when Date is inferred, tries ISO8601DateFormatter with and without fractional seconds. The helper reads nested Date fields too. Set an encoder date strategy separately when you need ISO text.

Boundaries to check before shipping

A model draft, not a full schema
No JSON Schema, OpenAPI, enum guessing, class, Core Data, SwiftData or networking code. Unknown JSON properties are ignored by synthesized decoding. Inferred dates pass calendar checks, but the exported helper is not a business date validator.
A conservative numeric budget
At most 38 significant digits after removing leading and trailing zeros; raw exponent −128 through 127; raw exponent minus fractional digit count at least −128; normalized decimal order at most 127. These tool limits apply inside mixed values too, including zero’s raw spelling. Other representations are rejected.
Verify Foundation on your target OS
Compilation and numeric roundtrips were tested with Swift 6.3.1 on macOS 26, also in Swift 5 language mode. This is not a test of an old compiler. Older Foundation releases may lose Decimal precision: test your deployment runtime. Numeric spelling such as 1.0 versus 1e0 is not preserved.
Dates represent instants
Only valid full timestamps with timezone and zero to three fractional digits become Date, and all non-null strings at that location must qualify. Date-only, naive, invalid and more precise samples remain String. Original timezone and formatting are lost; JSONEncoder has an independent date strategy. Inference accepts years 1583–9999; earlier timestamps stay String to avoid historical calendar shifts. The generated helper also rejects years before 1583.
Keys must remain distinguishable
Disabling CodingKeys rejects any needed renaming. Swift keywords, collisions and the CodingKeys property name are renamed safely. Lone surrogate characters in keys or values are rejected, as are distinct NFC-equivalent keys such as é and e plus a combining accent in one object or merged model.
Strict, bounded input
No comments, trailing commas, duplicate keys, automatic repair, file import, URL fetching or browser Swift execution. Limits: 500,000 input characters, 50,000 nodes, depth 40, 100 fields per merged object, 200 structs, 1,000 characters per key or numeric token and 1,000,000 output characters.

JSON to Swift questions

Can I decode the generated models with JSONDecoder?

Yes. Use makeJSONDecoder().decode(Root.self, from: data), or [Root].self for a root array. Keep its default key strategy because explicit CodingKeys already maps original names. JSON to Swift generates source here; compilation happens in your own Swift project.

Why does a generated property become optional?

A missing key in another sample or a null value makes the property optional. All-optional mode extends this to every property. Missing and null both become nil; add custom Codable logic if your protocol needs three distinct states.

Are ISO 8601 timestamps detected automatically?

No, the default keeps strings. Select ISO inference for complete timestamps with a timezone and up to millisecond precision. Generated Date properties require the included decoder helper, and date text will not roundtrip in its original spelling.

How are nested JSON objects represented?

Nonempty objects receive helper structs. Nested arrays merge all elements into one element type. Empty objects use string-keyed JSONValue dictionaries; mixed or unknown values use a recursive Codable enum.

How can Decimal retain the precision of JSON numbers?

The generator keeps original tokens and emits Int64 or Decimal within its stated budget. Real roundtrips were checked on Swift 6.3.1/macOS 26 using an independent decimal comparison. Foundation behavior varies across OS versions, so verify the same samples on your deployment target.

Found an inference case to improve?

Try contrasting API records

Use JSON to Swift with missing keys, nulls and representative numbers, then compile and decode the downloaded file in your actual deployment environment.