JSON to Swift
Generate Swift structs that conform to Codable from a JSON sample. Property names become camelCase with a CodingKeys enum when a key differs, optional and nullable values become optionals, and values of mixed type use a small JSONValue enum the tool adds to the output. Decode the result with JSONDecoder.
Frequently asked questions
How are nested objects named?
Every nested object becomes its own Swift type, named after the key that holds it in PascalCase, so address becomes Address. Objects inside arrays take the singular of the array key, so users becomes User. Objects with exactly the same fields share one type.
When is a field optional?
When an array holds several objects and a key is missing from some of them, that field is optional: its type becomes an optional, which Codable decodes as nil when the key is missing. A key present in every sample is required. A single object has nothing to compare with, so all of its keys are required.
How are null values handled?
A key that is null in some samples and has a value in others becomes an optional, such as String?. A key that is only ever null tells the tool nothing about its real type, so it becomes JSONValue?; paste a sample with a real value to get a precise type.
Which number types are used?
Whole numbers become Int and numbers with a fraction become Double. A field that holds both becomes Double.
What happens to keys that are not valid names?
Property names are converted to camelCase. When at least one name differs from its key, the struct gets a CodingKeys enum that maps every property to its JSON key. Names that are Swift keywords, such as default, are written in backticks.
Is my JSON uploaded or stored?
The JSON is sent to the server only to generate the code for this request and is not stored. The generator only reads the structure and the value types of your sample, so you can replace sensitive values with placeholders of the same type before you paste it.