JSON to Swift Codable
Generate Swift Codable structs with CodingKeys from a JSON object
About JSON to Swift Codable
Creates Codable structs with camelCase let properties and a CodingKeys enum only when a property name differs from its JSON key. Whole numbers map to Int, decimals to Double, nulls and keys missing from some array items to optionals, and mixed or empty values use a bundled JSONValue enum. Ready for JSONDecoder in iOS and macOS apps; keys present in the sample are required, and generation runs client-side.
Category: Converters.
Frequently asked questions
When does the output include a CodingKeys enum?
▾
Only when at least one property name differs from its JSON key, for example user_name becoming userName or my-key becoming myKey. Once the enum exists it lists every property, because Swift encodes and decodes only the keys it contains.
What is the JSONValue enum in the output?
▾
Swift has no built-in Codable type for arbitrary JSON, so a JSONValue enum is added whenever a value is null-only, of mixed types, an empty array or an empty object. It decodes strings, numbers, booleans, arrays, objects and null, and you can replace it with a concrete type once you know the shape.
How do I decode JSON with the generated structs?
▾
Use let root = try JSONDecoder().decode(Root.self, from: data), or decode RootList.self when the JSON is a top-level array. Leave keyDecodingStrategy at its default, because .convertFromSnakeCase rewrites keys before they are matched against the generated CodingKeys.
Why do some property names have backticks?
▾
Names that are Swift keywords, such as default, class or self, are wrapped in backticks so they compile as properties and still match their JSON keys. You can rename them, as long as the matching CodingKeys case keeps the original key as its raw value.