Paste a JSON object or API response and get ready-to-use Swift Codable structs with camelCase properties, CodingKeys, optionals and nested types. Everything runs in your browser.
Last updated:
Your JSON is parsed locally in this tab and never uploaded. Tip: Ctrl + Enter generates.
Advertisement
Advertisement
How to use the JSON to Swift Struct
Paste your JSON into the left box, or open a .json file. A sample is loaded so you can see the output right away.
Name the root struct, for example User or APIResponse.
Choose how property names are made: camelCase with CodingKeys, names for .convertFromSnakeCase, or the JSON keys as they are.
Set optionals, protocols and style: Codable or Decodable, let or var, public access, nested types, Date parsing.
Press Generate Swift Structs, then copy the code or download the .swift file and decode with JSONDecoder.
Example: JSON to Swift Codable structs
This API response has snake_case keys, a date, a nested array of objects and a field that is missing from one item:
struct Root: Codable {
let userID: Int
let displayName: String
let joinedAt: Date
let posts: [Post]
enum CodingKeys: String, CodingKey {
case userID = "user_id"
case displayName = "display_name"
case joinedAt = "joined_at"
case posts
}
}
struct Post: Codable {
let id: Int
let title: String
let score: Double
let coverURL: String?
enum CodingKeys: String, CodingKey {
case id, title, score
case coverURL = "cover_url"
}
}
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let root = try decoder.decode(Root.self, from: jsonData)
coverURL is optional because only one post has it, and score is Double because one value is 4.5.
How JSON values map to Swift types
JSON value
Swift type
"text"
String
"2024-05-01T09:30:00Z"
Date (with .iso8601, when the option is on)
42
Int
3.14, 1e6
Double
true / false
Bool
"x" or null, or a missing key
String?
{ … }
A nested struct
{}
[String: JSONValue]
[1, 2] / []
[Int] / [JSONValue]
[1, "two"] or always null
JSONValue
CodingKeys or convertFromSnakeCase?
Both approaches decode the same snake_case JSON. Explicit CodingKeys keep the mapping visible in the type, let you use Swift initialisms such as userID and coverURL, and work no matter how the decoder is configured. .convertFromSnakeCase keeps the structs shorter, but the property names must match its conversion exactly (user_id becomes userId), and every decoder that reads the type must set the strategy. Pick the matching option above and the generator names the properties for you.
Unknown keys are ignored by JSONDecoder, so you can delete properties you do not need.
A missing non-optional key throwsDecodingError.keyNotFound; make the property optional if the API sometimes omits it.
Identifiable: structs with an id property can adopt Identifiable for SwiftUI lists by adding it to the protocol list.
Frequently asked questions
When does the generator add a CodingKeys enum?
Swift's synthesized Codable matches property names to JSON keys exactly. When a property name differs from its key (for example userName for user_name), the struct needs a CodingKeys enum that maps every property to its key. The generator adds CodingKeys only to structs that need it and lists all of that struct's properties, because a CodingKeys enum must cover every stored property that is decoded.
What does the convertFromSnakeCase option do?
JSONDecoder can convert snake_case keys for you with decoder.keyDecodingStrategy = .convertFromSnakeCase. It removes underscores, lower-cases the first word and capitalises the following words, so avatar_url becomes avatarUrl (not avatarURL). This mode names properties exactly as that strategy produces them, so no CodingKeys are needed except for keys with characters such as dashes.
Which properties become optionals?
By default a property is optional (String?) when its value is null somewhere in the JSON or when the key is missing from some items of an array. Synthesized Codable decodes optionals with decodeIfPresent, so a missing key or null becomes nil instead of throwing an error. You can also make every property optional, which is safest for APIs you do not control.
How are dates handled?
Strings in the ISO 8601 internet date-time format, such as 2024-05-01T09:30:00Z, become Date when the option is on. Set decoder.dateDecodingStrategy = .iso8601 so JSONDecoder can read them. That strategy does not accept fractional seconds, so timestamps like 2024-05-01T09:30:00.123Z stay String; turn the option off to keep all dates as strings.
What is the JSONValue enum?
Swift has no Codable equivalent of Any, so a field whose type cannot be known (always null, mixed strings and numbers, or an empty object) uses a small JSONValue enum that is generated at the end of the code. It decodes any JSON value into cases such as .string, .number, .bool, .array, .object and .null and encodes it back.
How are numbers typed?
Whole numbers become Int, which is 64-bit on all current Apple platforms, and numbers with a decimal point or exponent become Double. If a field mixes whole and decimal numbers, Double is used.
Is my JSON uploaded?
No. Parsing and code generation are plain JavaScript running in this page, so API responses with personal or secret data never leave your device.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.