JSON Input

Swift Output

What Is JSON to Swift?

Swift uses structs and classes with Codable for JSON encoding and decoding. This tool generates Swift types from your JSON structure so you can use JSONDecoder and Codable for type-safe parsing. The JSON specification defines the input format.

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 the JSON Path tool. For the reverse (Swift/ObjC to JSON), you'd serialize in code. For validation, use the JSON Validator.

Frequently Asked Questions

Codable support?

The generator typically produces Codable types. Use JSONDecoder and JSONEncoder for serialization. Add CodingKeys if JSON keys differ from property names. See Apple's Codable guide.

Optional fields?

Optional JSON fields map to Optional<T> or T?. Enable Optional Types in the config for fields that can be null or missing. This lets you distinguish between absent and present values.

Struct vs class?

Structs are value types and are preferred for DTOs in Swift. The generator typically produces structs. Use classes if you need reference semantics or inheritance.

Is my data private?

Yes. Generation runs entirely in your browser. No JSON or code is sent to any server.

Can I use this with Alamofire?

Yes. Alamofire's response handlers work with Codable types. Generate the structs here, then pass them to responseDecodable(of: YourType.self).

Related Tools

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