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.
Type generation
Compare both records to see which properties become optional.
Parsed and generated in this browser. Input is not uploaded or stored.
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.
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.
Timestamps remain strings by default. ISO inference connects only valid, complete timestamps to Date properties and a matching decoder helper; other strings remain unchanged.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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?
Use JSON to Swift with missing keys, nulls and representative numbers, then compile and decode the downloaded file in your actual deployment environment.