About this tool
Paste JSON and get Swift types that conform to Codable, ready for JSONDecoder in iOS, macOS or server-side Swift. Property names are camelCase, and a CodingKeys enum maps them back to keys such as full_name. Values that were null or missing become optionals.
Decoding
let decoder = JSONDecoder()
let user = try decoder.decode(Root.self, from: data)
Turn off CodingKeys if you’d rather set decoder.keyDecodingStrategy = .convertFromSnakeCase, which converts every snake_case key automatically. Dates arrive as String; change the type to Date and set decoder.dateDecodingStrategy = .iso8601 to parse them.
Types
Whole numbers become Int (64-bit on all current Apple platforms), decimals Double, and arrays [T]. Fields whose type varies between samples become JSONValue, a small enum included in the output that can hold any JSON value. Prefer struct for models: it is a value type and immutable with let.
How types are inferred
- Every object becomes its own named type. Names come from the key (
address→Address), and items of an array get the singular (orders→Order). - For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes
nullbecomes nullable. - Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
- Identical nested shapes are reused instead of generating duplicates.
The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.
FAQ
How do I convert JSON to a Swift struct?
Paste the JSON above. A Codable struct is generated for each object, with CodingKeys for keys that aren’t valid camelCase names.
Do I need CodingKeys?
Only when property names differ from JSON keys. Alternatively, set keyDecodingStrategy = .convertFromSnakeCase on the decoder and turn the option off.
Why is a property optional?
It was null or missing in the sample. A non-optional property throws keyNotFound or valueNotFound when the key is missing or null.
How do I decode dates?
Change the property type from String to Date and set decoder.dateDecodingStrategy = .iso8601 (or a custom formatter).