JSON Input

Swift Output

What Is JSON to Swift?

Building an iOS or macOS app that calls a REST API? You'll get JSON back — but Swift needs explicit Codable structs to decode it safely. Writing those structs by hand for every API endpoint is repetitive and error-prone. This tool generates Swift structs from your JSON automatically, complete with Codable conformance. Just paste a sample response and you'll get ready-to-use types for JSONDecoder right away.

Conversion runs in your browser. You can set the struct name and enable Use Codable and Optional Types. Nothing is sent to a server. The generated types are a starting point for iOS, macOS, or server-side Swift projects.

When JSON to Swift Helps

When building iOS or macOS apps that consume REST APIs, generate Codable structs here. Use JSONDecoder to parse. Paste a sample response to get typed models.

How to Use This Tool

1

Paste or Upload JSON

Paste your JSON into the left editor or upload a file. Use Sample for example data. Set the root Struct Name in the config. Enable Use Codable for Codable conformance and Optional Types for optional fields.

2

Review the Generated Types

The right panel shows Swift structs or classes. Nested objects become nested types. Use JSONDecoder().decode(YourType.self, from: data) to parse. Add CodingKeys if JSON keys differ from property names. For invalid JSON, use the JSON Validator first.

3

Copy or Download

Use Copy or Download. For formatting JSON first, use the JSON Formatter. For validation, use the JSON Validator.

JSON to Swift Examples

Here is an example of generating Swift structs from a JSON object.

Example: Subscriber record

JSON input:

Input

Generated Swift output:

Output

When JSON to Swift Helps

When building iOS or macOS apps that consume REST APIs, you need Swift types for the response payload. Pasting a sample response here gives you Codable structs you can use with URLSession or Alamofire. Server-side Swift (Vapor, Kitura) benefits from the same workflow. Manually writing structs for complex nested JSON is error-prone; this tool infers the structure from your sample. For pulling out specific values from large responses first, use jq from the command line. For the reverse (Swift/ObjC to JSON), you'd serialize in code. For validation, use the JSON Validator.

Frequently Asked Questions

What is a Codable struct in Swift?

Codable is a typealias for Encodable & Decodable. A Codable struct can be converted to/from JSON using Swift's built-in JSONDecoder and JSONEncoder. It's the modern, recommended way to handle JSON in iOS and macOS apps. See Apple's Codable guide.

How do I decode JSON in Swift?

Create a Codable struct that matches your JSON, then call JSONDecoder().decode(YourType.self, from: data). Generate the struct here — paste your JSON and copy the result into your Xcode project.

Struct vs class for JSON models in Swift?

Structs are value types and are preferred for DTOs and JSON models in Swift — they're simpler, thread-safe, and play well with Codable. Use classes when you need reference semantics or inheritance.

Is my JSON data sent to a server?

No. The struct generation runs entirely in your browser. Your JSON never leaves your device, so it's safe for API keys or sensitive payloads.

Can I use this with Alamofire?

Yes. Alamofire's responseDecodable(of: YourType.self) works with any Codable type. Generate the struct here and plug it straight into your Alamofire response handler.

Related Tools

Swift Codable. JSONDecoder. JSON spec. RFC 8259. MDN.