Skip to content
XUtilHub

JSON to Swift Codable Struct Generator

Convert JSON to Swift Codable structs or classes with CodingKeys for renamed fields, optionals for nullable values and a JSONValue type for unknown data.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

About the JSON to Swift

Generate Swift Codable models from JSON. Properties use camelCase, CodingKeys map them back to the original JSON keys, and optional or nullable values become Swift optionals.

Key features

  • Swift structs or final classes conforming to Codable
  • CodingKeys generated only when names differ from keys
  • camelCase properties; Swift keywords escaped with backticks
  • Optionals for nullable and missing values
  • let or var properties
  • A small JSONValue enum for values of unknown type

Swift Codable models from JSON

iOS, macOS and server-side Swift apps decode JSON with Codable. Paste a sample response and get Codable structs with the right property types, optionals and CodingKeys.

Example

[
  { "user_id": 7, "name": "Ada", "nickname": "ada", "score": 4.5 },
  { "user_id": 8, "name": "Alan", "nickname": null, "score": 3 }
]
struct User: Codable {
    let userId: Int
    let name: String
    let nickname: String?
    let score: Double

    enum CodingKeys: String, CodingKey {
        case userId = "user_id"
        case name
        case nickname
        case score
    }
}

typealias UserList = [User]
let users = try JSONDecoder().decode([User].self, from: data)

Notice that score became Double because one item had 4.5, and nickname became optional because one item had null.

Type mapping

JSONSwift
"text"String
42Int
3.14Double
trueBool
[...][T]
{...}struct
null or missingT?
unknownJSONValue?

Tips

  • Dates: change String to Date and set decoder.dateDecodingStrategy = .iso8601.
  • SwiftUI: add Identifiable (and Hashable) conformance to use models directly in List and ForEach.
  • Errors: decoding errors name the exact key path. Print error in a catch to see it.

Building the Android app as well? Use JSON to Kotlin. For Flutter, see JSON to Dart.

How to use the JSON to Swift

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The Swift appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

Why are there CodingKeys?

Swift properties use camelCase, but JSON keys may use snake_case or other styles. CodingKeys map each property to its exact JSON key. They're only generated when at least one name differs.

Could I use keyDecodingStrategy instead?

Yes. For consistently snake_cased JSON, you can delete CodingKeys and set decoder.keyDecodingStrategy = .convertFromSnakeCase. Explicit CodingKeys also handle irregular keys like "first-name".

Struct or class?

Structs are the Swift default for data models: value semantics, thread-safe copies and memberwise initializers. Use final classes when you need reference semantics or Objective-C interop.

What is JSONValue?

A small Codable enum generated when some value's type couldn't be inferred (null, mixed or empty). It decodes any JSON safely, so decoding never fails on those fields.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools