How to use the json to swift struct converter
- Paste your JSON into the input area.
- Optionally change the Root struct name to match your domain model.
- The generated Swift structs appear instantly as you type.
- Copy the output and paste it directly into your Xcode project.
How the json to swift struct conversion works
The tool parses your input with JSON.parse and walks the resulting tree. Each JSON object becomes a Swift struct conforming to Codable. Properties keep the order of the keys in your JSON.
Primitive types map directly: strings become String, integers become Int, floating-point numbers become Double, and booleans become Bool. If a key contains characters that are not valid Swift identifiers, such as hyphens or leading digits, a CodingKeys enum is generated automatically to map the JSON key to a camelCase property.
Type inference rules
When the input is an array of objects, the converter merges all keys found across every element. For each key, it inspects the values to determine the most specific type. If one object has an integer and another has a double for the same key, the result is Double.
| JSON value | Swift type |
|---|---|
"hello" | String |
42 | Int |
3.14 | Double |
true | Bool |
null | JSONValue? |
[1, 2] | [Int] |
{...} | Nested struct |
A null value without any other non-null examples leaves the type ambiguous, so the generator marks it as optional JSONValue? with a comment and appends a small Codable JSONValue enum to the output, because Swift's own Any cannot be decoded. Provide at least one non-null example in your JSON array to get a precise type.
Handling nested objects and arrays
Nested objects are extracted into their own structs. The struct name is derived by converting the parent key to PascalCase. For example, a key called billing_address produces a struct named BillingAddress.
Arrays of objects are handled similarly. The element type becomes a new struct, and the property type is wrapped in brackets. If the array is empty, the element type defaults to JSONValue because there is no data to infer from.
This approach mirrors how you would manually write models when using tools like json to go struct or json to kotlin data class, keeping your codebase idiomatic regardless of the target language.
CodingKeys and snake_case mapping
Swift convention uses camelCase for property names, while many JSON APIs use snake_case. When the converter detects a key that needs transformation, it generates a nested CodingKeys enum inside the struct.
For instance, the JSON key created_at becomes the Swift property createdAt, and the enum maps them:
case createdAt = "created_at"
If all keys are already valid Swift identifiers and need no transformation, the CodingKeys enum is omitted entirely. This keeps the generated code clean and lets the compiler synthesize the coding logic automatically.
Common edge cases
Mixed-type arrays: If an array contains both strings and numbers, the inferred type falls back to [JSONValue]. Consider normalizing your JSON before conversion if possible.
Unicode keys: Characters other than ASCII letters and digits are dropped from property names, and the original key is kept in the CodingKeys enum so decoding still works. A key with no usable characters becomes a property called field.
Empty input: Pasting an empty string or whitespace returns an error message rather than generating invalid Swift code.
If you need to convert your data to other formats first, tools like json formatter or csv to json can help prepare your input.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple user | JSON input: {"name": "Ada", "age": 36, "active": true}; Root struct name: User | import Foundation / struct User: Codable { / let name: String / let age: Int / let active: Bool / } |
| Nested object | JSON input: {"id": 1, "profile": {"bio": "Hello", "score": 9.5}}; Root struct name: Account | import Foundation / struct Profile: Codable { / let bio: String / let score: Double / } / struct Account: Codable { / let id: Int / let profile: Profile / } |
| Array of items | JSON input: [{"sku": "A1", "price": 12.99}, {"sku": "B2", "price": 5.50}]; Root struct name: Product | import Foundation / struct Product: Codable { / let sku: String / let price: Double / } |
FAQ
Does this support Swift Codable?
Yes. Every generated struct conforms to the Codable protocol, which combines Encodable and Decodable. You can use the structs directly with JSONEncoder and JSONDecoder.
What happens if my JSON has null values?
If a key is null in some objects but has a typed value in others, the property is marked as optional with that type. If it is always null, the type becomes JSONValue? with a comment suggesting you provide a concrete example.
Can I convert a JSON array?
Yes. Paste a JSON array of objects and the converter will merge all keys across the elements to produce a single comprehensive struct definition.
Is my data sent to a server?
No. The json to swift struct conversion runs entirely in your browser using JavaScript. Nothing is uploaded anywhere.
How are nested objects named?
Nested object keys are converted to PascalCase to form the child struct name. For example, a key called shipping_info produces a struct named ShippingInfo.
Why does my output have a CodingKeys enum?
The CodingKeys enum appears only when a JSON key cannot be used directly as a Swift identifier, such as keys with hyphens or snake_case names that are mapped to camelCase properties.