JSON1 / JSON to Swift
JSON to Swift
Generate Swift structs conforming to Codable from a JSON sample. CodingKeys are emitted only when a key differs from its property name, so simple models stay readable.
Processed locally·Your data never leaves your device
InputJSON
ReadyOutputSwift
Converted output appears here as you type.
Good to know
- Every record in an array is inspected, not just the first, so a field that appears in only some of them is typed as optional rather than required.
- Identical nested shapes are emitted once and reused, and reserved words are renamed to something legal in this language.
About this tool
Conformance to Codable is the whole of JSON decoding in Swift — no annotations, no library, and no generated decoder, provided the property names line up with the keys. This produces the structs, let properties throughout, and a CodingKeys enum for the cases where the names do not line up.
Swift's mapping is the tightest of the nine targets, because the language has an optional type and a synthesised initialiser and needs neither a builder nor an annotation to use them:
IntJSON integers, not Int64. Int is 64-bit on every platform Swift ships on, and the shorter spelling is what Swift code actually uses.DoubleAnything with a fractional part. Codable will happily decode a JSON integer into a Double, so a field that is sometimes 1 and sometimes 1.5 is safe.String?Optional or nullable. Codable treats an absent key and an explicit null identically for an optional property, which is either exactly what you want or a distinction you will need to make by hand.AnyCodable?A field with no observable type. Not a standard library type — see below.letEvery property. A decoded response is a value, and the memberwise initialiser comes free.
- AnyCodable is not in the standard library. What do I do with it?
- Right — it is a placeholder for a field that was null or empty in every record, and there is nothing in Foundation that means "any JSON value". Either replace it with the concrete type once you have a populated sample, or add one of the small AnyCodable implementations from the community. It is left in deliberately: emitting
String?would be a guess presented as a fact. - Why do only some structs have a CodingKeys enum?
- It appears only when at least one key differs from its property name. Where they match, Codable synthesises the mapping and the enum is dead weight — and the enum is all-or-nothing, so one mismatched key means listing every case.
- Why a struct rather than a class?
- A decoded response is a value: two responses with the same fields are the same response, and nothing should be mutating one through a second reference. Structs give you that, plus the memberwise initialiser. Reach for a class only when you genuinely need identity or inheritance.
- Will this handle a snake_case API without CodingKeys?
- It will, and then you do not need the enum at all: set
decoder.keyDecodingStrategy = .convertFromSnakeCaseand Codable does the renaming. Worth knowing before pasting a CodingKeys block with thirty cases in it — though the enum is more explicit, and it survives a key that the strategy converts wrongly. - How are optional fields decided?
- Every record in an array is inspected, not just the first. A field that appears in some records and not others is typed as optional, and a value that is sometimes null widens accordingly. Generators that read only the first element get this wrong on exactly the payloads where it matters.
- What happens to a key that is a reserved word?
- It is renamed to something legal in the target language, and where the language supports it, an annotation records the original JSON key so serialisation still round-trips.
Related tools
- JSON to TypeScriptGenerate TypeScript interfaces from a JSON sample. Fields missing from some records are marked optional, and values that are sometimes null widen to a null union.
- JSON to JavaGenerate Java classes with Jackson annotations from a JSON sample. Optional primitives are boxed, so an absent field stays distinct from zero or false.
- YAML to JSONConvert YAML to formatted JSON. Handles nested mappings, block and inline sequences, quoted scalars, and comments.