Loading JSON to Swift Converter...
Please wait a moment

JSON to Swift: Codable Structs for JSONDecoder

Paste the JSON your app really receives and get Swift structs whose types match it, from optional fields to IDs too long for an Int.

Step 1

Paste or upload your JSON

Start with a real response from your API, ideally one where every field shows up at least once. A key that never appears in your JSON sample can't appear in the structs, and a list with several items tells the page which fields are optional.

Example input

{
  "userId": 8901240544102066246,
  "username": "iosdev",
  "email": "[email protected]",
  "isActive": true,
  "preferences": ["dark_mode", "push_notifications", "face_id"],
  "profile": {
    "first_name": "iOS",
    "last_name": "Developer",
    "age": 25,
    "country": "USA"
  }
}
Step 2

Set the struct name and options

Three settings sit above the editors. Everything else follows from your data.

•
Struct Name: the name of the top-level struct and of the downloaded .swift file
•
Use Codable Protocol: adds Codable conformance and the CodingKeys enums; untick it for plain structs
•
Make Every Property Optional: off by default, so only fields that are null or missing somewhere get a question mark
•
Types: Int, then Int64, UInt64 or Decimal for whole numbers; Double for anything with a decimal point; quoted values like "00501" stay String
Step 3

Read the generated structs

This is the full output for the example above. The long userId became Int64, and only Profile needed CodingKeys, because its snake_case keys don't match the Swift names.

Output for the example above

// Read it with:
//   let data = try JSONDecoder().decode(Root.self, from: jsonData)

import Foundation

struct Root: Codable {
    let userId: Int64
    let username: String
    let email: String
    let isActive: Bool
    let preferences: [String]
    let profile: Profile
}

struct Profile: Codable {
    let firstName: String
    let lastName: String
    let age: Int
    let country: String

    enum CodingKeys: String, CodingKey {
        case firstName = "first_name"
        case lastName = "last_name"
        case age
        case country
    }
}
Step 4

Copy or download the .swift file

Drop the file into your target and decode the response where you call your API, for example with URLSession's data(from:) and JSONDecoder.

•
Copy puts the whole file on your clipboard
•
Download saves it as a .swift file named after your top-level struct
•
Top-level lists: the comment at the top shows the [Root].self form for decoding a JSON array
•
Check the data first: the JSON Validator points at the exact line of a syntax error

What is JSON to Swift Conversion?

Swift reads JSON by decoding it into types that conform to Codable. Writing those structs by hand for a big response is tedious, and the mistakes only show up at runtime: a field you thought was always there, an ID that overflows Int on a 32-bit device, or a key like last_name that never matches lastName without a CodingKeys entry.

This page writes the structs from a real sample instead. If your data is XML, the XML to Swift page does the same job, and the JSON Formatter tidies a minified response before you paste it.

Frequently Asked Questions

Can JSONDecoder read the generated structs straight away?

Yes, as long as "Use Codable Protocol" is ticked. The structs only use Foundation, and the comment at the top of the file shows the decode call, including the [Root] form for a JSON array. With the box unticked you get plain structs, and any key that needed renaming will no longer be matched.

Why is a long ID an Int64 and not an Int or a String?

The type comes from how each number is written in your JSON, across every item of a list. Whole numbers that fit in 32 bits become Int, bigger ones become Int64, so an ID like 8901240544102066246 keeps every digit, and positive numbers up to 18446744073709551615 become UInt64. Anything longer becomes Decimal. A number with a decimal point or exponent, such as 14.50 or 1e3, becomes Double. Text in quotes, like "00501", stays a String so the leading zeros survive.

When does a property become Optional?

When its value is null somewhere in your JSON, or when it is missing from some items of a list. JSONDecoder then accepts the key being absent or null. Everything else stays non-optional, which means decoding fails if a later response leaves it out. If your API is loose about which fields it sends, tick "Make Every Property Optional".

How are keys like first_name or class turned into Swift names?

Keys are split into words and joined in camelCase, so "first_name" becomes firstName and a CodingKeys case maps it back to the original key. Swift keywords such as class or default are written in backticks, self and init get Value added, and keys that start with a digit get a field prefix. Names that would collide, like "first-name" and "first_name", get a number, and struct names that would hide String, Data or Decimal get Model added.

What happens with values whose type changes between items?

If a field is a number in one item and text in another, or is always null or an empty list in your sample, there is no single Swift type for it. Those fields get a small JSONValue enum, written at the bottom of the file, that holds whatever JSONDecoder finds and writes it back unchanged. Numbers inside it keep every digit as Int64 or UInt64. A fuller sample usually gives you plainer types.

Is my JSON sent anywhere?

No. The structs are generated in your browser as you type, and nothing you paste or upload is sent to our server or kept after you leave the page.