CruxDevCruxDev Tools Studio Prompts
dev Utility • Zero-Server Privacy • 100% Client-Side

JSON to Swift Codable Struct

Turn JSON into Swift Codable structs with CodingKeys for snake_case keys, optionals for missing or null fields and nested types, ready to decode with JSONDecoder.

Overview

Turns a JSON sample into Swift structs that conform to Codable, with camelCase property names, a CodingKeys enum for snake_case keys, optionals for fields that can be missing or null, and nested structs for objects and arrays. The result decodes directly with JSONDecoder. Everything runs locally, so private API responses stay on your machine.

How It Works

Paste JSON or start from the sample, set the root struct name and choose let or var properties. CodingKeys can be generated only when a property name differs from its JSON key or for every struct, and optionals can follow the data or apply to every property. The Swift code refreshes on each keystroke; copy it or download a .swift file.

Step-by-Step Usage Guide

  1. Paste the JSON your app receives, using several items when it is a list.
  2. Name the root struct and choose let for immutable models or var for editable ones.
  3. Decide whether CodingKeys should appear only when needed or always.
  4. Copy the structs or download the .swift file into your Xcode project.
  5. Decode with the JSONDecoder line shown at the end of the output.

Technical Specifications & Standards

Swift is strict about decoding: a non-optional property whose key is missing makes JSONDecoder throw keyNotFound, which is the most common reason hand-written models fail. To avoid that, every element of an array is merged before types are chosen, and any field absent from one element or null in any element becomes optional. Integers map to Int and any fractional number to Double, booleans to Bool and strings to String, or to Date when you turn on ISO 8601 detection, in which case you also need decoder.dateDecodingStrategy = .iso8601. Property names are camelCased, and a name that is a Swift keyword, such as class, default or protocol, is wrapped in backticks. When a field is always null or an empty array its type cannot be known, so it is typed JSONAny, declared at the bottom as a String typealias you should replace. A commented decode line at the end shows whether to decode a single struct or an array.

Targeted Use Cases

  • Writing Codable models for a REST API in an iOS or macOS app.
  • Typing a bundled JSON configuration file for a SwiftUI preview.
  • Prototyping response models in a Swift Playground before wiring networking.
  • Converting snake_case backend payloads without setting a global key decoding strategy.

Notes & Gotchas

  • Feed the generator a list with varied items so optional fields are marked correctly.
  • Replace every JSONAny placeholder with a concrete type before shipping.
  • Prefer let properties for models that come straight from the network.
  • Turn on Date detection only together with a matching dateDecodingStrategy on your decoder.

Frequently Asked Questions

When does it add a CodingKeys enum?

With the default setting, only when at least one property name differs from its JSON key, for example userId for user_id. Choose Always if your team prefers explicit keys on every struct.

Why are some of my properties optional?

A property becomes optional when its key is missing from at least one sample object or its value is null. Making it non-optional would cause JSONDecoder to throw when that case appears in real data.

How are Swift keywords such as class handled?

Reserved words are escaped with backticks, so the struct declares let `class`: String and the CodingKeys case is written the same way, which Swift accepts as a normal identifier.

What is the JSONAny type in the output?

It marks a field whose type could not be inferred because it was always null or an empty array. It is declared as a String typealias so the code compiles; change it to the real type.

Does it handle a JSON array at the root?

Yes. The element structs are generated from all array items merged together, and the decode hint at the end uses [Root].self so you decode the whole array in one call.

Related tools

  • JSON → Kotlin Data Class - Generate Kotlin data classes from JSON with nullable types, default values, and kotlinx.serialization annotations.
  • JSON to C# Class Generator - Generate C# classes or records from JSON with System.Text.Json or Newtonsoft attributes, nullable types and nested classes inferred from every array item.
  • JSON to TypeScript - Generate typed TypeScript interfaces from any JSON — nested objects, arrays, and unions handled automatically.
  • JSON → Go Struct Generator - Generate idiomatic Go structs with proper json tags, type inference, nested struct extraction, and optional omitempty.
  • JSON → Rust Struct (Serde) - Generate Rust structs with #[derive(Serialize, Deserialize)] and proper serde attributes from JSON samples.
  • JSON → Python Dataclass - Generate Python @dataclass or TypedDict definitions from JSON samples with proper type hints and Optional fields.