jsontoolskit.org
JSON and JSON Schema utilities, in the browser
Say hi →

JSON to Swift

JSON sample → Codable structs · updated 28 July 2026

Generate Swift Codable types from a JSON sample. Properties are camelCased, and when any key differs from its property name a CodingKeys enum is emitted so JSONDecoder maps them correctly. Optional and nullable fields become Swift optionals.

Paste input to start.

How to generate Swift Codable structs from JSON

  1. Paste a JSON sample — ideally an array of several records, so optional fields are detected.
  2. Set the Root type name. Nested objects are named after the key that holds them, singularised for arrays.
  3. Keep struct unless you need reference semantics or inheritance.
  4. Tick public if the types live in a framework target that app code must import.
  5. Copy the code, or download it as a file.

Type mapping

Dates and key strategies

Swift will not turn an ISO 8601 string into a Date unless you ask. Change the property type to Date and set decoder.dateDecodingStrategy = .iso8601.

If every key in your API is snake_case, you can delete the generated CodingKeys entirely and use decoder.keyDecodingStrategy = .convertFromSnakeCase instead. Keep the explicit enum when the naming is inconsistent — which, in real APIs, it usually is.

Getting good types out of a sample

Type inference from a sample is only as good as the sample. Three habits make the output far more useful:

Privacy

Nothing is uploaded. Inference and code generation run in this tab. No server sees the payload you paste — which matters, because real API responses tend to contain real user data.

FAQ

Why is CodingKeys missing on some types?

It is only emitted when at least one property name differs from its JSON key. When they all match, the synthesised conformance is correct and an explicit enum would be noise.

Should optional fields be Int? or use decodeIfPresent?

Declaring the property optional is enough — the synthesised initialiser calls decodeIfPresent for optionals automatically. You only write a custom init(from:) for defaults or transformations.

Can I get Objective-C compatible classes?

Not directly. Switch to class, then add @objc and inherit from NSObject by hand — Codable and Objective-C bridging do not overlap cleanly.