JSON to Swift
Swift Codable structs with CodingKeys from a JSON sample.
About this tool
Paste JSON and get Swift Codable structs ready for JSONDecoder. Choose let or var properties and optionally add Hashable conformance, then copy the code into Xcode.
How the Swift is generated
Each JSON object becomes a struct conforming to Codable, and each key a camelCase property (user_name โ userName). Whenever a name differs, a CodingKeys enum maps it back to the JSON key. Nested objects become their own structs, arrays [T], whole numbers Int, decimals Double and booleans Bool. Array items are merged before types are chosen, so a key that only some items have, or that is sometimes null, is treated as optional or nullable rather than breaking decoding. Those become optionals (String?), so decoding doesn't throw. Swift keywords are escaped with backticks. We compiled the generated code for the sample with swiftc and decoded the JSON with JSONDecoder to check it works. Honest limits: types describe your sample. A field that's always null is marked as an unknown optional for you to fix, and dates arrive as String unless you set a dateDecodingStrategy. JSON to Kotlin, JSON to Dart and the JSON formatter are related.
Frequently asked questions
How do I convert JSON to a Swift struct?
Paste the JSON here and copy the Codable structs, then decode with JSONDecoder().decode(User.self, from: data).
Why is there a CodingKeys enum?
It maps Swift's camelCase property names to the original JSON keys, such as user_name.
Can I use convertFromSnakeCase instead?
Yes, for snake_case APIs you can delete CodingKeys and set keyDecodingStrategy = .convertFromSnakeCase.
Is my JSON uploaded?
No. Everything runs in your browser and nothing is stored or sent.