JSON to Swift Codable
Codable structs with CodingKeys mappings, one struct per nested object — paste-ready.
Consuming an API in iOS starts with turning the JSON response into models: Swift's Codable convention is camelCase fields, while APIs overwhelmingly return snake_case or hyphenated keys — hand conversion means renaming fields, writing CodingKeys and hoping the nesting is right. This tool does it automatically: every nested object becomes its own struct, and non-identifier keys get explicit CodingKeys.
Field types infer from the JSON values: Int for integers, Double for decimals, Bool, String, and [T] for arrays. An array root produces a typealias list type with the element model taken from the first item.
CodingKeys is the life raft for messy keys
A JSON key user_name should read userName in Swift — and only when the two differ does the struct need an enum CodingKeys: String, CodingKey mapping. The tool generates CodingKeys only when necessary (hyphens, spaces, non-identifier characters), so clean keys stay clean.
How the Swift output differs from other targets
Codable synthesizes its implementation at compile time — there is no annotation baggage like Java/Lombok, so the output is the purest possible struct declaration. Optional handling is intentionally conservative: fields emit as non-optional let; mark them var + ? yourself where the API may omit them.
Frequently asked questions
- Does the generated code need third-party libraries?
- No. Codable is part of the Swift standard library; JSONDecoder handles the runtime side.
- How are array roots handled?
- The first element defines the element model, and typealias RootObjectList = [RootObject] is emitted; mark optionals yourself where the first item omits fields.
- What about purely numeric or symbol-laden keys?
- Field names normalize into legal identifiers (illegal characters stripped, leading-lower camelCase), while CodingKeys preserves the original key exactly — parsing is unaffected.