formatjson.pro
Validate, pretty-print or minify JSON in your browser — nothing is uploaded.

JSON to Swift Codable Struct

Paste a sample JSON object below and generate Swift structs conforming to `Codable`. The tool infers a type for every field, creates a named struct for each nested object, and adds a `CodingKeys` enum when a JSON key is not a valid Swift identifier.

Runs entirely in your browser — your JSON is never uploaded to a server. No ads injected into the output, no sign-up. Emits Codable structs with a CodingKeys enum for non-identifier keys, so JSONDecoder works immediately.

How to generate a Swift Codable struct from JSON

  1. Paste your JSON. Paste one representative JSON object into the box above.
  2. Generate the struct. Click Generate Swift struct. The tool infers a type for every field and emits a Codable struct for the root and each nested object.
  3. Decode with JSONDecoder. Copy the structs in and call JSONDecoder().decode(Root.self, from: data).

How the type mapping works

Each JSON value maps to a Swift type: a whole number becomes Int, a decimal becomes Double, a string becomes String, true/false becomes Bool, and null becomes String?. Arrays become [T]. When a key is not a valid Swift identifier, the tool emits a CodingKeys enum mapping the property back to the raw JSON key.

Arrays and nested objects

An array becomes a typed array using the type of its first element, and every nested object gets its own Codable struct so the result compiles as-is. Mark fields that may be absent as optional with ?.

Using the result

Decode with JSONDecoder: let root = try JSONDecoder().decode(Root.self, from: data).

FAQ

Do the structs conform to Codable?

Yes. Every generated struct conforms to Codable, so it works with both JSONDecoder and JSONEncoder out of the box.

When is a CodingKeys enum added?

Only when a JSON key is not a valid Swift identifier (for example it contains a dash). The tool sanitizes the property name and maps it back with CodingKeys so decoding still matches the raw key.

How do I handle optional fields?

A single sample cannot show which fields are missing elsewhere. Mark fields that may be absent as optional (Type?) — JSONDecoder sets a missing key to nil.