Skip to content
shellhacks.dev
Tooling

JSON to Swift Codable Generator

Generate Swift structs conforming to Codable from a JSON sample, with camelCase properties, CodingKeys for renamed keys and optionals for missing values.

Free · No sign-up · Your inputs stay in your browser

Input

Generate Swift types from JSON

Output

Swift

488 characters

Structs conform to Codable. A key that is not a camelCase Swift name gets a CodingKeys entry, so JSONDecoder reads it without a global keyDecodingStrategy. Nullable or missing keys are optionals. Types come from this one sample: give fields that are null here a real value to type them.

Runs in this browser. Nothing you paste is sent to a server.

CodingKeys or a decoding strategy

JSONDecoder.keyDecodingStrategy = .convertFromSnakeCase converts snake_case keys for every type at once, which saves writing CodingKeys, but it applies to the whole decoder and cannot handle keys that are not snake_case. Explicit CodingKeys, as generated here, work with any decoder settings. Apple's Encoding and Decoding Custom Types article covers both.

Dates and numbers

Timestamps arrive as String here; set decoder.dateDecodingStrategy = .iso8601 and change the property to Date to decode them directly. Whole numbers become Int and fractions Double.

Share

Frequently asked questions

Why does decoding fail with keyNotFound?
A non-optional property's key was missing from the JSON. Make the property optional, or give the struct a custom init(from:) with decodeIfPresent and a default.
Should I use let or var?
let suits values decoded once and passed around. Use var if the app edits the model before encoding it again.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Go deeper

Suggested reading

Keep planning

Similar tools