Skip to content

Menu

JSON to Swift

Convert JSON to Swift Codable structs

Runs entirely in your browserConvertersFree, no sign-up
Loading tool

01What is JSON to Swift?

Writing Codable structs by hand for every API response gets old fast. Paste a JSON sample here and you get Swift structs that conform to Codable, the protocol Apple's Foundation uses with JSONDecoder and JSONEncoder. Each property is a let, typed from your sample: String, Int for whole numbers, Double for decimals and Bool. Nested objects become their own structs, named after their key in PascalCase and listed before the struct that uses them. Objects with exactly the same fields share one struct. So if home and work both hold lat and lng, you get one Home struct used twice. Names that would shadow standard types get a Model suffix, which is why a data key produces DataModel and not a struct that hides Foundation's Data. Keys are turned into valid camelCase property names. avatar_url becomes avatarUrl, first.name becomes firstName, and 2fa becomes _2fa. Swift keywords such as default or class are wrapped in backticks. Whenever any property name differs from its key, a CodingKeys enum maps each one back to the original JSON key, following the pattern in Apple's Encoding and Decoding Custom Types guide. Arrays become [Type] when every element agrees. Mixing 1 and 2.5 gives [Double], and an array like [1, null, 3] gives [Int?]. A null value on its own becomes String?, because the sample can't tell us the real type. Empty arrays and arrays of different shapes can't use [Any], since [Any] isn't Codable. They become [JSONValue] instead, and a small Codable JSONValue enum is added to the top of the output. A quick example: {"user_id": 1, "is_admin": false, "score": 4.5} gives struct Root with userId: Int, isAdmin: Bool and score: Double, plus a CodingKeys enum with case userId = "user_id" and case isAdmin = "is_admin". A top-level array such as [{"id": 1}] with Struct name Rows becomes struct RowsItem plus typealias Rows = [RowsItem]. There are limits you should plan for. Every property is non-optional unless the sample held null, so a key the API sometimes omits will throw a DecodingError. A number that happened to be whole in your sample becomes Int even if the API sends decimals later. Dates and URLs stay as String. Objects in one array that differ by even one key fall back to [JSONValue]. The conversion happens in the page itself, so your JSON never leaves the tab.

iOS and macOS developers who want Codable models for a JSON API without typing every property and CodingKey by hand. The tool runs entirely in your browser, so your data never leaves your device.

02How to use JSON to Swift

  1. Paste a sample JSON payload into JSON Input.
  2. Enter the top-level type name in Struct name (Root by default).
  3. Read the generated structs, CodingKeys and any JSONValue helper in Swift Output.
  4. Click Copy and add the code to a Swift file in your Xcode project.
  5. Replace String? and [JSONValue] placeholders with real types where you know them.

03Worked examples

Input: {"user_id":1,"is_admin":false,"score":4.5}
Result: struct Root: Codable { let userId: Int let isAdmin: Bool let score: Double enum CodingKeys: String, CodingKey { case userId = "user_id" case isAdmin = "is_admin" case score } }
snake_case keys become camelCase properties, and CodingKeys maps them back.
Input: {"default":1,"tags":[],"extra":null}
Result: enum JSONValue: Codable { … } struct Root: Codable { let `default`: Int let tags: [JSONValue] let extra: String? }
default is a keyword, so it gets backticks. The empty array adds the JSONValue enum (shortened here), and null becomes String?.
Input: {"data":{"first.name":"A","2fa":true},"home":{"lat":1.5,"lng":2},"work":{"lat":3.25,"lng":4}} (Struct name: User)
Result: struct DataModel: Codable { let firstName: String let _2fa: Bool enum CodingKeys: String, CodingKey { case firstName = "first.name" case _2fa = "2fa" } } struct Home: Codable { let lat: Double let lng: Int } struct User: Codable { let data: DataModel let home: Home let work: Home }
data becomes DataModel to avoid shadowing Foundation's Data. home and work share one Home struct.
Input: [{"id":1},{"id":2}] (Struct name: Rows)
Result: struct RowsItem: Codable { let id: Int } typealias Rows = [RowsItem]
A top-level array becomes a typealias over an Item struct.
Input: {"ids":[1,null,3],"vals":[1,2.5]}
Result: struct Root: Codable { let ids: [Int?] let vals: [Double] }
Nulls inside an array make the element optional, and mixed Int and Double widen to Double.
Input: {"items":[{"id":1},{"id":2,"name":"x"}]}
Result: enum JSONValue: Codable { … } struct Root: Codable { let items: [JSONValue] }
The items have different keys, so no Item struct is made. Give every sample element the same keys to get a typed struct.

04Common use cases

  • Modeling a REST response before writing the URLSession call in an iOS app
  • Generating models for a Firebase or Supabase JSON export used in a SwiftUI view
  • Decoding a bundled settings.json file in a macOS menu bar utility
  • Wrapping a third-party SDK that hands back raw JSON dictionaries
  • Showing new Swift developers how CodingKeys maps snake_case keys to camelCase properties
  • Checking which fields in a payload will need optional types before you write decoding tests

05Tips

  • If one sample has lng: 2 and the API later sends 2.5, decoding fails because lng became Int. Use a sample with decimals, or change it to Double.
  • You can drop the CodingKeys enum and set decoder.keyDecodingStrategy = .convertFromSnakeCase, as long as every key is plain snake_case.
  • Objects in an array that differ by one missing key fall back to [JSONValue]. Give the sample items the same keys, then mark the sometimes-missing property optional.
  • Dates arrive as String. Change the property to Date and set decoder.dateDecodingStrategy = .iso8601 for ISO 8601 timestamps.
  • JSONEncoder leaves out nil optionals rather than writing null, so a decode and re-encode round trip can drop keys.

06Related concepts

Codable, CodingKeys, JSONDecoder, Swift structs, camelCase.

Frequently Asked Questions

What is JSON to Swift?

JSON to Swift is a free, browser-based developer tool on AIDToolStack. Convert JSON to Swift Codable structs It requires no installation or sign-up and works entirely in your browser.

Is JSON to Swift free to use?

Yes. No sign-up, no fees and no usage cap.

Does JSON to Swift store my data?

No. It runs in your browser; nothing is uploaded.

Reviews and commentsShow

Reviews

Sign in to leave a review.

Loading reviews…

Comments

Sign in to leave a comment.

Loading comments…

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe