YAML to Swift
Paste YAML and instantly generate matching Swift Codable struct definitions with inferred property types.
Example
A YAML document with nested objects and arrays maps onto Swift structs:
name: Acme
version: 2
author:
name: Jane
verified: true
tags:
- ios
- mobile
becomes:
struct Root: Codable {
let name: String
let version: Int
let author: Author
let tags: [String]
}
struct Author: Codable {
let name: String
let verified: Bool
}How it works
The tool parses your YAML with js-yaml, infers a type model from the values (including nested objects and arrays), and emits idiomatic Swift structs conforming to Codable. The root type is named Root.
Good to know
YAML to Swift takes a YAML document and generates matching Swift struct definitions that conform to Codable, inferring each property's type from the actual values in your input. It's aimed at iOS, macOS, and server-side Swift developers who get config files, API fixtures, or sample payloads in YAML and want a ready-to-paste model layer instead of hand-writing every property and its type.
Reach for it when you're scaffolding a decoder for a YAML config, prototyping against a sample response, or porting a schema from another stack. Paste your YAML on the left and press Convert; the output mirrors your structure, naming the top-level type Root and creating a separate named struct for each nested object (for example, an author block becomes an Author struct). Arrays of objects are collapsed into a single struct describing all elements, so one model covers the whole list.
Read the result type by type. Scalars map predictably: true/false becomes Bool, whole numbers become Int, decimals become Double, and everything else falls back to String. A trailing ? on a property means the value was missing or null in at least one sampled element, so the field is optional. A CodingKeys enum appears when YAML keys aren't valid Swift identifiers (hyphens, spaces) and had to be renamed to lowerCamelCase, preserving the original key for decoding.
A practical caveat: type inference is only as good as your sample. If a field that's genuinely optional happens to be present in every example you paste, it will be generated as non-optional; include a representative element where the field is absent or null to get the ? you expect. Mixed types across samples (other than Int plus Double, which widen to Double) default to String, and empty arrays default to [String] since there's nothing to infer from.
Frequently asked questions
How does it decide which properties are optional?
When you convert an array of objects, any key that is missing or null in at least one element becomes an optional (Type?). Keys present and non-null in every sample stay non-optional.
Why do some structs include a CodingKeys enum?
YAML keys with characters that are not valid Swift identifiers (like hyphens or spaces) are converted to lowerCamelCase property names, and a CodingKeys enum is emitted to map them back to the original YAML keys for Codable.
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.
People also ask
Does the generated Swift code require import Foundation?
Only if your model uses Foundation types such as Date. The tool emits a commented-out import Foundation line; basic structs with String, Int, Double, and Bool compile with the standard library alone, but uncomment it if you adapt the model to use Date or other Foundation types.
Can it convert YAML into Swift classes instead of structs?
No. The tool always generates value-type structs conforming to Codable. If you need reference semantics or inheritance, you would change struct to class manually after generating the model.
How are nested objects and arrays of objects handled?
Each nested object becomes its own named struct, and an array of objects is merged into a single struct that includes every key seen across the elements. Keys absent in some elements are marked optional.
What happens if my top-level YAML is a list rather than a mapping?
If the document is an array of objects, the tool generates a Root struct and types the result as [Root]. If it's an array of scalars or mixed values, it emits a typealias such as typealias Root = [String].
Does it support Codable decoding from JSON too, since YAML is a JSON superset?
The generated structs conform to Codable, so they can decode from any compatible Decoder, including JSONDecoder, as long as the JSON keys match the struct properties or their CodingKeys mapping. The tool itself only parses YAML input.
Why did a number in my YAML become a String instead of Int or Double?
That usually happens when a field holds different value types across the samples (for example, a number in one element and text in another), which the tool resolves to String. The exception is mixing Int and Double, which widens to Double.
Can I rename the Root struct the tool generates?
Not within the tool; the top-level type is always named Root. You can rename it in your editor after copying the output, updating any references and the [Root] array typealias if present.
Related tools