Online Tool Store Online Tool Store
📱 Developer

· 4 min read

How to Generate Swift Codable Structs From JSON

Heshan Fernando

Co-founder & COO

Heshan Fernando is the Co-founder and Chief Operating Officer of Ceyentra Technologies, where he leads project management, engineering, and research and development strategy. With over nine years of industry experience, he is passionate about transforming complex customer challenges into practical, high-impact solutions. His customer-centric leadership has enabled multidisciplinary teams to consistently deliver secure, scalable, and industry-grade digital products that create lasting business value. View on LinkedIn

Share

How to Generate Swift Codable Structs From JSON

You’ve just gotten a sample response back from an API you’re integrating into an iOS app, and now you need to hand-write a matching Codable struct — mapping every field to the right Swift type, handling nested objects as their own structs, and writing a CodingKeys enum because the API returns snake_case fields but Swift convention wants camelCase properties. For a response with a dozen fields and a couple of nested objects, that’s a tedious, error-prone transcription exercise before you’ve written a single line of actual app logic.

It’s exactly the kind of mechanical translation task that’s easy to get subtly wrong by hand — a mistyped key name in CodingKeys fails silently at decode time rather than at compile time, which makes it an annoying bug to track down later.

What generating Codable structs actually involves

Swift’s Codable protocol needs a struct whose properties match the JSON’s structure, with correct Swift types for each value (String, Int, Double, Bool, arrays, or nested custom types), and — when the JSON’s field names don’t already match Swift’s camelCase convention — a CodingKeys enum mapping each Swift property to its actual JSON key. Nested JSON objects need their own separate Codable structs, which is where hand-writing gets especially repetitive for deeply nested API responses.

Why people get stuck here

  • Guessing types from a single sample. A field that’s null or a whole number in your one sample JSON might actually be optional or a decimal in other responses, and a generator working from one example can only infer so much.
  • snake_case to camelCase mapping errors. Manually writing CodingKeys for a dozen fields is exactly the kind of repetitive task where a single typo compiles fine but fails silently at decode time.
  • Forgetting nested objects need their own struct. It’s easy to flatten a nested JSON object into the parent struct by mistake, which then doesn’t actually match the real response shape.
  • Not handling optional fields. A field that’s sometimes present and sometimes missing in the API response needs to be an optional property, or decoding will crash on the responses where it’s absent.

What a good Codable generator looks like

Correctly maps nested JSON objects to their own structs

Generating a separate, properly named struct for each nested object — not flattening it — keeps the generated code an accurate match for the actual response shape.

Builds a correct CodingKeys enum automatically

Automatically converting snake_case JSON keys to camelCase Swift properties and generating the matching CodingKeys enum removes the most error-prone manual step.

Infers reasonable Swift types from sample values

Mapping JSON numbers, strings, booleans, and arrays to their natural Swift equivalents gives you a solid starting point you can refine rather than a blank struct to fill in from scratch.

Common mistakes to avoid

  • Treating generated structs as final without checking against the API’s actual documented schema, since a single JSON sample can’t capture every field’s full range of possible values.
  • Forgetting to mark fields optional that the API might omit in some responses, which causes decode failures you won’t catch until they hit production.
  • Not double-checking numeric types — a field that looks like an integer in one sample response might actually return decimals in others.
  • Skipping a review of the generated CodingKeys mapping, especially for fields with unusual naming that doesn’t cleanly convert between conventions.
  • Reusing a generated struct across multiple slightly different API endpoints without checking that the shape genuinely matches each one.

How to do it with Swift Codable Generator

Online Tool Store’s Swift Codable Generator generates your structs entirely in your browser.

  1. Open the Swift Codable Generator tool.
  2. Paste a sample JSON object from the API response you’re working with.
  3. Get back matching Swift Codable structs, including nested types and a CodingKeys enum.
  4. Copy the generated code into your project and adjust optionality or types based on the API’s actual documentation.

Frequently asked questions

Will the generated struct always be exactly correct?

It’s a strong starting point based on your sample JSON, but since it can only see the one example you provide, always cross-check against the API’s actual documentation for fields that might be optional, or that might return different types across different responses.

Does this handle deeply nested JSON?

Yes — nested objects are generated as their own separate Codable structs rather than being flattened, matching how you’d structure it by hand for a response with multiple levels of nesting.

Why does Codable need a CodingKeys enum at all?

Swift property names conventionally use camelCase, while many APIs return snake_case JSON keys. CodingKeys maps each Swift property to its actual JSON key name so Codable can encode and decode correctly without renaming your properties to match the API’s convention.

Final thought

Hand-writing Codable structs for anything beyond a trivial response is tedious in a way that adds no real value — get the mechanical translation done automatically, then spend your actual attention reviewing edge cases against the real API docs.

Try the free Swift Codable Generator tool

#swift codable generator#json to swift struct#swift codable from json#generate swift model#online-tools#free-tools