JSON to Swift Converter
Convert JSON into Swift Codable structs with proper types, nested structs, optionals, and CodingKeys. Free, instant, and runs entirely in your browser.
Why Generate Swift Structs from JSON?
Every iOS and macOS app that talks to an API eventually needs models that match the JSON it receives. Writing those structs by hand is slow and error prone: key names rarely follow Swift conventions, nested objects explode into multiple types, and a single wrong type breaks decoding at runtime with a cryptic error.
This converter reads a JSON sample and produces Swift Codable struct definitions that match its shape. Paste a response from your API, name the root type, and copy the generated code straight into Xcode.
How Types Are Mapped
Each JSON value is translated into the Swift type that decodes it correctly:
- Strings become
String, whole numbers becomeInt, and decimal numbers becomeDouble. - Booleans become
Bool. - Arrays become Swift arrays typed to their element, such as
[String]or[Product]. - Nested objects become separate named structs, so a nested
addressobject turns into its ownAddresstype referenced by the parent. - Keys that are sometimes missing or null become optionals, so
String?is used where data may be absent.
CodingKeys and Snake Case APIs
Swift style favors camelCase properties, but most APIs return snake_case keys like created_at or user_id. The generator follows that convention: user_id becomes a property named userId, and an explicit CodingKeys enum maps it back to the original JSON key. That means JSONDecoder works without any keyDecodingStrategy configuration, and the generated code is readable at the call site.
When every key is already a valid Swift identifier, no CodingKeys enum is emitted, keeping simple models compact.
Using the Generated Code
- Paste a representative JSON sample. One object with realistic values is enough, and including optional fields helps the generator mark them optional.
- Set the root struct name, for example
WeatherResponseorUserProfile. - Copy the output into a Swift file in your project alongside your networking code.
- Decode with
try JSONDecoder().decode(WeatherResponse.self, from: data).
Structs are generated with let properties and value semantics, which matches Swift's recommendation for data models. If you need mutable models, change let to var after pasting.
Sample Data Tips
- Include one entry for every field, even if you have to add it by hand, so no column is missing from the output.
- Use the real value types from your API. A numeric ID that arrives as a string should stay a string in the sample.
- For arrays, one representative element is enough. The generator infers the element type from it.
- If a field is nullable in production, include a null value somewhere so it becomes optional.
Privacy
All parsing and code generation happens in your browser. API responses often contain tokens, user data, or internal identifiers, and none of it is uploaded, stored, or logged. The tool also works offline once the page has loaded.
Frequently Asked Questions
Does the generated code use Codable?
Yes. Every struct conforms to Codable, which combines Encodable and Decodable, so the models work with both JSONDecoder and JSONEncoder out of the box.
Why does my output include a CodingKeys enum?
The enum is added when the JSON keys are not valid Swift property names, such as snake_case keys or keys with dashes. It maps the camelCase property back to the exact JSON key so decoding succeeds without extra configuration.
How are optional and null values handled?
Fields that are missing in some objects, or null in the sample, are generated as optionals using the question mark syntax, for example String?. Fields that are always present with a value are non optional.
What JSON input does the tool accept?
Any valid JSON: an object, an array, or a nested combination of both. An array of objects produces a single element struct that you can decode as an array, for example [Product].
Can I use the output in SwiftUI and Combine projects?
Yes. The structs are plain Codable value types with no third party dependencies, so they flow naturally through URLSession, async await, Combine publishers, and SwiftUI state.
Is my JSON uploaded anywhere?
No. Conversion runs entirely in your browser using a bundled parser. Your data never leaves your device, is never stored, and is never logged.
Related Tools
Working with other languages or formats? These tools pair well with this converter:
Related Tools
Recent Visits
Finance Calc Kit
Focused calculators on Finance Calc Kit for loans, compounding investments, contractor tax estimates, and reti...
Explore Finance Calc Kit