YAML to JavaScript Class Generator
CV-90Paste a YAML document and press Convert to get ES classes or constructor functions. The YAML is read into JSON first, so the types come from its values, and it runs in your browser.
Runs in your browser. Nothing is uploaded.
YAML
JavaScript
How to use it
- Paste one YAML document, or open a .yaml or .yml file. Use a sample that shows every section you expect.
- Choose an ES class or a constructor function, and keep or drop the JSDoc comments.
- Press Convert, then copy the result or download it as a .js file.
- Build the model with new Root(JSON.parse(jsonText)). The names follow the JSON form of your YAML, so add the tags your YAML library needs if you load YAML directly.
How the classes are built
Each object becomes an ES class, or a constructor function if you choose one, whose constructor copies the keys from a parsed JSON object. JSDoc comments give each type, with |null for null keys and a note on keys missing from some objects. Nothing is exported, and no getters, setters or validation are written. Nested objects, and lists of objects, are turned into instances of their own classes.
How the YAML becomes classes
A value written as 1.0 is read as the number 1, and an integer beyond 9007199254740991 is rounded by the YAML reader. .nan and .inf become null. The YAML is parsed with the core schema, converted to JSON, and the code is built from that JSON. true/false, null, integers and floats get their usual types, while dates stay strings. Keys that are not valid JavaScript names, such as first-name, are renamed, and the original key is kept in the data["key"] read in the constructor.
What a sample cannot tell it
The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay strings, and an integer beyond 9007199254740991 is already rounded by JSON.parse before the class sees it. A YAML file with optional sections should be pasted with every section present, or the missing sections will not appear in the output.
Questions people ask
How do I convert YAML to a JavaScript class?
Paste the YAML into the input box, choose your options and press Convert to get the classes. Build the model with new Root(JSON.parse(jsonText)). The classes follow the JSON form of the YAML. The conversion happens in your browser.
Does the JavaScript converter handle keys that are not valid names?
Yes. A key such as first-name is read with data["first-name"] and stored under the camelCase property firstName. Keys that are already valid identifiers keep their name. A key named constructor or __proto__ gets a trailing underscore. This applies to the JSON form of your YAML.
Why are there JSDoc comments, and can I remove them?
They give editors a type for each property, for example number, or string|null for null keys, plus a note on keys that are missing from some objects. Turn the JSDoc option off to get bare classes with no comments. This applies to the JSON form of your YAML.
Class or constructor function for the JavaScript output?
The ES class is the modern choice and the default. Pick the constructor function if you target older environments or prefer the plain function style. Both copy the same keys and build nested objects into instances of their own classes. This applies to the JSON form of your YAML.
Does it keep YAML comments, anchors and merge keys?
Comments are dropped because they are not data. Anchors, aliases and << merge keys are resolved, so the values they point to appear in the types. A document that contains an alias to itself is rejected with an error. The JavaScript classes are written from that converted data.
Why did a YAML value like 1.0 come out as an integer type?
The YAML is converted to JSON before types are chosen, and 1.0 is the same number as 1 there. A field that always holds whole-looking decimals in your sample can come out as an integer type. Add a non-whole decimal to the sample, or change the type by hand. The JavaScript classes are written from that converted data.
About this tool
The YAML is read with the core schema (true/false, null, integers and floats), converted to JSON, and the code is generated from that. Dates stay text, comments are dropped, anchors, aliases and << merge keys are resolved, keys that are not text become text, and only one document is read. A value such as 1.0 is read as the number 1, so a field that holds whole-looking decimals can come out as an integer type; an integer beyond 9007199254740991 is rounded by the YAML reader before any code is written; .nan and .inf become null. Every object becomes an ES class (or a constructor function) whose constructor copies the keys from a parsed object, and nested objects and lists of objects are turned into instances of their own classes. Keys that are valid identifiers are used as property names; other keys are renamed to camelCase and read with data["key"]. JSDoc comments give each type, and an integer beyond 9007199254740991 is typed number even though JSON.parse rounds it. Missing keys stay undefined. There are no getters, setters or validation, and the output has no export line, so add one to match your module system. Everything runs in your browser; nothing is uploaded.