JSON to Swift Struct
Paste JSON and generate clean, ready-to-paste Swift Codable structs with nested models, optionals, and CodingKeys.
Input JSON
Generated Swift
Your generated Swift structs will appear here.About JSON to Swift Struct
This free tool turns any JSON object or array into idiomatic Swift structs that conform to Codable in one click. It inspects your JSON, infers the most specific type for every field, and generates strongly-typed models you can paste straight into an iOS, macOS, watchOS, or server-side Swift project.
It picks the right Swift type for each value — Int for whole numbers, Double for decimals, Bool, and String — and builds nested structs for inner objects and array types like [Order]. Fields that are null or only present in some array items become optionals (Type?) so decoding never fails on missing data. JSON keys in snake_case are converted to camelCase property names, and a CodingKeys enum is generated so JSONDecoder still maps to the original keys.
Choose Codable, Decodable, or Encodable conformance, add Hashable or Equatable, map ISO-8601 strings to Date, and mark everything public for framework targets. Everything runs entirely in your browser, so your JSON never leaves your device.
How to Use JSON to Swift Struct
- Paste a JSON object or array into the input box, or click Load Sample to try it.
- Set the root struct name for the generated model.
- Pick a protocol (
Codable,Decodable, orEncodable) and addHashable/Equatableif needed. - Choose a date type, toggle
let/var,CodingKeysgeneration, andpublicaccess to match your style. - Copy the generated Swift and paste it into your project.
null or hold mixed types default to String? as a safe placeholder — replace them with the correct type, or with a custom AnyCodable wrapper, if needed.What it handles
- ✅
Codablestructs - ✅ Nested structs
- ✅ Optionals (
Type?) - ✅ Arrays as
[T] - ✅
CodingKeysfor renamed keys - ✅
snake_case→camelCase
FAQ
Does this tool send my JSON to a server?
No. All parsing and code generation happens in your browser, so your JSON stays on your device.
What is a CodingKeys enum and when is it added?
It tells Codable how to map Swift property names to JSON keys. The tool adds one whenever a key like created_at becomes a camelCase property such as createdAt, so decoding still finds the original key. You can also force it on for every struct.
Can I skip CodingKeys and use a decoder strategy instead?
Yes. Turn off Generate CodingKeys and set decoder.keyDecodingStrategy = .convertFromSnakeCase on your JSONDecoder to map snake_case automatically.
When does a property become optional?
When its value is null, or when it is missing from some objects in an array. Optional properties use the Type? syntax so decoding tolerates absent values.
Does it support root-level JSON arrays?
Yes. A root array generates a typealias Root = [Item] plus a struct for the element shape.
How do I decode dates as Date?
Select Date as the date type, then set a matching dateDecodingStrategy (for example .iso8601) on your JSONDecoder.