Generate Swift models for a JSON response
JSON to Swift infers Swift model types from a JSON example. It provides an initial shape for a client that decodes API data.
When should you use it?
- Prototype a Swift response model.
- See how nested records might map to structs.
- Start a typed representation of a JSON fixture.
Example: infer Swift fields from JSON
This abbreviated example shows the inferred name and active fields in Swift. The full generated file includes imports and helpers and may change with generator versions; review it before adding it to a project.
{"name":"Alex","active":true}struct Root: Codable {
let name: String
let active: Bool
}Review decoding behavior as well as properties
A useful Swift model needs the correct wire-key mapping and decoding behavior alongside its property types. Inspect the complete generated declarations and helpers before copying them into a project. A JSON name that does not match an idiomatic Swift property may need CodingKeys or the corresponding decoder strategy.
Make optional and date behavior explicit
A sample cannot prove whether a field is always present. Check optionals against the API contract and test missing values separately from null. A string that looks like a date also needs an agreed format and decoding strategy before it becomes a Date in your application. Keep identifiers textual when preserving their exact spelling is more important than numeric operations.
How do I generate Swift Codable models from a JSON sample?
Generate Swift source and inspect the inferred struct and CodingKeys. A representative sample helps reveal nested arrays and scalar types, but date strings, absent fields, and server variants still need application-level review.
Illustrative Swift Codable model
swiftstruct Root: Codable {
let name: String
let active: Bool
}The full .swift output can include convenience initializers. Align decoder settings and optional properties with your app before use.
Will a JSON date string automatically become a Swift Date?
A sample cannot establish the intended date format or timezone policy. Review generated string or date types and configure decoding explicitly.
What should you check before using the result?
Review CodingKeys, optional properties, dates, numeric types, and your decoder configuration before shipping. The example cannot reveal fields it never contains.
Understand JSON syntaxTechnical reference: quicktype model generation documentation