JustRandom

JSON to Swift Codable Converter

Generate Codable structs and classes from JSON.

Generate Swift from JSON

Paste sample JSON and generate Swift models locally in your browser.

Use an object or an array of objects for model inference.

Swift options

Generated code

Generated code will appear here.

Convert JSON to Swift

Convert a JSON sample into Swift structs or classes with typed stored properties. Codable conformance can be included so the result fits native JSONEncoder and JSONDecoder workflows.

Nested objects become named Swift models. When a normalized Swift property name differs from its JSON key, the converter adds CodingKeys so decoding still uses the original payload.

Swift Codable declarations and key mapping

Choose struct for value semantics or class for reference semantics, then select internal or public access. Properties can use immutable let declarations or mutable var declarations.

Optional output adds a question mark to fields that are missing or nullable in the samples. Codable and CodingKeys are emitted together when key mapping is necessary; already-safe matching keys do not receive redundant mappings.

Swift conversion example

Example JSON input:

{
  "article_id": 9,
  "author": {
    "display_name": "Ada"
  },
  "published": true
}

Generated Swift output:

struct RootModel: Codable {
  let articleId: Int
  let author: RootModelAuthor
  let published: Bool

  enum CodingKeys: String, CodingKey {
    case articleId = "article_id"
    case author
    case published
  }
}

struct RootModelAuthor: Codable {
  let displayName: String

  enum CodingKeys: String, CodingKey {
    case displayName = "display_name"
  }
}

How to generate Swift code

  1. Paste a JSON object or an array of similar objects.
  2. Enter the root Swift model name.
  3. Choose struct or class output and adjust access, mutability, optional-property, Codable, and property-name options.
  4. Generate the Swift code, then copy it or download the .swift file.

Frequently asked questions

How does JSON become a Swift Codable struct?

The converter infers Swift property types, creates a struct by default, and adds Codable conformance when that option is enabled.

When are CodingKeys generated?

CodingKeys appear when a safe generated property name differs from the original JSON key, such as converting article_id to articleId.

Should I use struct or class output?

Use a struct for value semantics and independent copies. Choose a class when the model needs reference identity or class-specific behavior.

How are nullable fields represented?

When optional properties are enabled, fields that are null or absent in some samples receive optional Swift types using a question mark.

How are nested models generated?

Each nested object becomes a named Swift declaration. The parent property uses that type, and the nested declaration receives its own CodingKeys when needed.

Private browser-based conversion

Your JSON input, field names, root model name, and generated Swift source stay in this browser. They are not sent to a backend, added to the URL, or saved in local storage or cookies.

Related tools

Compare another language-specific converter that uses the same local JSON inference engine.

Back to JSON to Code overview