How JSON maps to Swift
| JSON | Swift |
|---|---|
| "text" | String |
| 42 | Int |
| 3.5 | Double |
| true | Bool |
| null, or missing in some items | an optional, like String? |
| […] | [Element] |
| {"plan": {…}} | a nested struct Plan |
| "user_id" | userId, with a CodingKeys case |
Decoding it
let root = try JSONDecoder().decode(Root.self, from: data) print(root.userId)
Dates arrive as String, because JSON has no date type. To decode them as Date, change the property type and set decoder.dateDecodingStrategy = .iso8601 (or a custom formatter for other formats).
