JSON Input

Swift Output

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. Paste a JSON sample and you get one struct per 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 struct gets a CodingKeys enum that maps every property to its original key, 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, fields that were null or missing in the sample become optionals, which Codable decodes as nil when the key is absent. Untick Codable for plain structs. It all runs in your browser.

How to use it

The structs update as you type.

1

Paste a JSON sample

Paste into the Input panel, Upload a .json or .txt file, or click Sample. Example input:

{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}

Set Struct Name for the root struct. If the input won't parse, check it with 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: fields that were null, or missing from some objects in an array, become optionals. Off: they keep the plain type, so decoding throws if the value really is missing. Fields typed JSONValue already have a .null case.

How are large numbers handled?

The page reads integers exactly. An ICCID like 8901240544102066246 fits in Int; positive values up to 18446744073709551615 become UInt64, and anything bigger becomes Decimal.

What if the JSON root is an array?

The objects inside it become the root struct and a comment shows the [RootObject].self to decode. A root that is null, a string or a list of plain values gives a short message instead of code.

Is my JSON uploaded?

No. The structs are generated by JavaScript in your browser and nothing is sent to a server. Input is capped at 256 KB.

Other JSON Tools You Might Need

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