JSON to Swift Converter

Swift Codable structs generated from a JSON sample

JSON Input

Swift Output

Paste your JSON on the left and the Swift code shows up here.

Or drop a file on this box, , or .

What does the JSON to Swift converter generate?

JSONDecoder does the heavy lifting in Swift, but you still have to write the structs it decodes into. Feed it a sample response and it writes a struct for each object, conforming to Codable, with let properties in camelCase and nested types listed first. Swift keywords used as keys, such as default or class, are wrapped in backticks.

When a property name has to differ from its JSON key (user-id becomes userId, 2fa becomes _2fa), the generator adds a CodingKeys enum that pairs each property with the JSON key it came from, as described in Encoding and Decoding Custom Types. Structs whose keys all map directly skip the enum. Whole numbers are Int (64-bit on every current Apple platform), UInt64 for positive values past that, and Decimal beyond. Arrays become [T], nested arrays [[T]].

Any isn't Codable, so values whose type varied, or that were only ever null, are typed with a small JSONValue enum that the output defines for you, with cases for string, number, bool, object, array and null and its own init(from:) and encode(to:). An empty object becomes [String: JSONValue]. With Optional Types on, any property that was null somewhere, or absent from one of the objects, becomes an optional, which Codable decodes as nil when the key is absent. Untick Codable for plain structs. Nothing is compiled or uploaded; the Swift source is produced in the page.

How to use it

The structs update as you type.

1

Paste a JSON sample

Paste into JSON Input, upload a .json or .txt file, or try Sample. A weather forecast response works well, and it hides a trap:

{"city":"Bergen","tempHigh":9.5,"tempLow":4,"precipitationMm":12.3,"warnings":[]}

Struct Name sets the root struct. Name it Forecast and notice that tempLow comes out as Int, because the sample said 4; if tomorrow's response says 4.5, JSONDecoder throws, so change it to Double. The empty warnings array becomes [JSONValue]. If the output shows Invalid JSON, run the text through the JSON Validator.

2

Read the structs

The Output panel shows import Foundation, the structs with nested ones first, and the JSONValue enum at the end if it was needed.

3

Copy or download

Copy the code or Download a .swift file, then let root = try JSONDecoder().decode(RootObject.self, from: data).

Where this saves time

URLSession responses

Paste a response from your backend and decode URLSession data straight into typed models instead of casting [String: Any] dictionaries.

SwiftUI previews and fixtures

Generate the model from a sample JSON file, bundle the file, and decode it in your previews so the UI shows realistic data.

Server-side Swift

Vapor and Hummingbird routes accept Codable types, so a generated struct can serve as the request body for a JSON endpoint.

Messy third-party APIs

Keys with dashes or leading digits, fields that are sometimes null, values that change type: the generated CodingKeys, optionals and JSONValue cover them so decoding doesn't fail on the first odd record.

Common Questions

Has the output been compiled?

It follows Swift's rules for keywords, string escapes and Codable synthesis, but there is no Swift compiler behind this page. Build it once in Xcode or with swiftc, and replace any JSONValue fields with real types once you know them.

When does a struct get a CodingKeys enum?

Only when at least one property name differs from its JSON key. The enum then lists every property, since Swift requires CodingKeys to cover all of them.

What does Optional Types change?

On: a property that held null in one place, or that some array element left out, is declared optional and decodes to nil. Off: the plain type stays, and JSONDecoder throws (keyNotFound or valueNotFound) the first time that value is missing. Properties typed JSONValue can already hold .null.

How are large numbers handled?

Big integers are read without rounding. A 15-digit IMEI or a 19-digit ICCID such as 8901240544102066246 fits Swift's 64-bit Int; positive values up to 18446744073709551615 are typed UInt64, and anything larger gets Decimal.

My endpoint returns an array of forecasts. What do I get?

One struct built from the array's objects, plus a comment telling you what to decode, such as [Forecast].self. If the array holds only plain values, say ["Bergen", "Oslo"], there are no properties to declare, and the panel tells you so instead of printing an empty struct.

Is my JSON uploaded?

No. The structs are built by the page in your browser, so the response you paste stays on your machine. The cap is 256 KB, and Codable types only need the shape, so a few array entries are plenty.

Other JSON Tools You Might Need

Need the same models in Kotlin or Objective-C, or want to clean up the JSON first?