Generate Swift Structs from API Response
This example generates Swift Codable structs from a typical REST API response. The pre-filled JSON represents a user object with nested address, array of roles, and array of post objects. Switch to Swift mode to see the generated structs.
What You Get
The tool produces:
- A root
Rootstruct conforming toCodable RootAddressstruct for the nested address objectRootPostsItemstruct for array itemsCodingKeysenum when property names differ from JSON keys- camelCase property names following Swift conventions
- Optional types (
T?) for optional fields - Swift type mappings:
String,Int,Double,Bool,[String]
How to Use
- The example loads with a sample API response and Swift mode pre-selected
- Inspect the generated Swift structs and their Codable conformance
- Copy the structs and paste them into your Swift project
- Replace with your actual API response to generate structs for your endpoints
Real-World Pattern
Swift Codable structs are the standard way to decode JSON in iOS and macOS apps. The generated code works directly with JSONDecoder().decode(T.self, from: data). Use optional types for fields that may not always be present in the response.