Tool Kumar tool
JSON to Swift models
Generate Swift structs that conform to Codable. The converter adds CodingKeys when property names differ from the JSON, so a key such as user-id still decodes into userId.
How to use this tool
- Paste a representative response, including records whose fields differ.
- Review the generated output and change the root name if needed.
- Copy or download the code, then check the types against your API contract.
Worked example
This output comes from the built-in converter. The two item prices infer a floating-point field; the null and string notes infer a nullable field.
Input
{
"user-id": 42,
"profile": {
"city": "London",
"created_at": "2026-10-14T09:00:00Z"
},
"items": [
{
"price": 12,
"note": null
},
{
"price": 12.5,
"note": "gift"
}
]
}Output
import Foundation
struct Root: Codable {
let userId: Int
let profile: Profile
let items: [Item]
enum CodingKeys: String, CodingKey {
case userId = "user-id"
case profile, items
}
}
struct Profile: Codable {
let city: String
let createdAt: String
enum CodingKeys: String, CodingKey {
case city
case createdAt = "created_at"
}
}
struct Item: Codable {
let price: Double
let note: String?
}
Decode with JSONDecoder
Use JSONDecoder with the generated Root struct. Codable also supports encoding the same model with JSONEncoder.
Keep the default key strategy for this output. Its CodingKeys already contain wire names, including created_at.
let model = try JSONDecoder().decode(Root.self, from: responseData)
let encoded = try JSONEncoder().encode(model)Options and edge cases
- Integers infer Int, fractional values infer Double and integers beyond signed 64-bit range infer Decimal. Check target-platform limits and decimal precision.
- Nulls or missing fields become optional properties. A null-only or mixed shape uses the generated JSONValue helper when needed.
- JSONValue stores numbers as Double, so use explicit types for exact monetary values or very large IDs.
- Date strings remain String. If you change a field to Date, configure JSONDecoder.dateDecodingStrategy for the API format.
Private by default
Your document stays in this browser. Tool Kumar parses, converts and creates downloads on your device, with no account or upload step.
JSON to Swift models questions
- Does it generate classes or structs?
- Structs conforming to Codable, with let properties and nested model types. It imports Foundation.
- When does it add CodingKeys?
- When a property name changes, it emits a CodingKeys enum that maps Swift identifiers back to the original JSON keys.
- How does it handle null?
- Observed nulls and missing fields produce optional properties. Unknown or mixed values use a Codable JSONValue enum, added only when referenced.
- Can I use JSONDecoder directly?
- Yes. Copy the generated structs into your project and decode Root.self from Data. Review optional fields and date types first.
Related tools
JSON to Dart, JSON to Kotlin data classes, JSON Schema generator.