JSON to Swift Converter
Generate Swift Codable structs from a JSON sample — optionals, nested types and CodingKeys, ready for JSONDecoder.
Codable without the ceremony
Swift's Codable is elegant right up until you hand-write the fifth nested struct and its CodingKeys enum. The generator takes a JSON sample and emits the whole tree: structs conforming to Codable, optionals where the sample shows null or missing fields, [Element] arrays, and CodingKeys wherever the wire name (snake_case, kebab-case) differs from the Swift-cased property.
Straight into JSONDecoder
let decoder = JSONDecoder()
let playlist = try decoder.decode(Playlist.self, from: data) That's the entire integration — the generated types carry everything the decoder needs. For ISO-8601 timestamps set decoder.dateDecodingStrategy = .iso8601 and switch the corresponding property from String to Date; the generator leaves date-looking values as String because JSON itself makes no promise.
Optionals mirror your sample's honesty
A property is non-optional only if the sample never showed it null or absent — the same contract Swift enforces at compile time. For models that guard real app screens, generate from a payload that includes the edge cases, not the happy-path demo response. Prettify and inspect it in the formatter or the tree viewer before generating; thirty seconds of sample hygiene saves a decoding crash in TestFlight.
Frequently asked questions
Are CodingKeys generated automatically?
Yes, wherever needed: snake_case wire names map to camelCase Swift properties via a CodingKeys enum per struct. If every key already matches, no enum is emitted — the code stays minimal.
struct or class?
Structs — value semantics are the Swift default for model data and what the standard library, SwiftUI and most style guides expect. Converting a specific type to a class (reference semantics) is a local decision you can make afterwards.
How do I decode dates?
Timestamps arrive as String because JSON has no date type. Set decoder.dateDecodingStrategy = .iso8601 (or a custom formatter) and change the property type to Date — the one manual step the sample cannot decide for you.
Does this work with SwiftUI previews and tests?
Perfectly — Codable structs decode fixture JSON in previews and unit tests with the same three lines as production code. Keep the sample you generated from as the fixture.