YAML to JSON Schema Generator
CV-91Paste a YAML document and press Convert to get a draft 2020-12 schema with types and required keys. 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
JSON Schema
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 the indent, whether keys in every object are required, additionalProperties: false, and format detection.
- Press Convert, then copy the result or download it as a .json file.
- Validate documents with any validator that supports draft 2020-12, and tighten the schema by hand where the sample was too loose. The names follow the JSON form of your YAML, so add the tags your YAML library needs if you load YAML directly.
How the schema is built
The schema declares the draft 2020-12 dialect in $schema and gives every value a type: object, array, string, integer, number, boolean or null. Arrays get an items schema merged from every item, and an empty array gets none. Format detection can add date-time, date, time, email, uuid, uri or ipv4 to strings, and additionalProperties: false can be switched on. Keys that appear in every object of the sample go into required, and keys missing from some objects stay optional.
How the YAML becomes a schema
Anchors, aliases and << merge keys are resolved, so the values they point to appear in the generated types. Comments are dropped, only one document is read, and keys that are not text become text. 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. Keys that are not valid JSON Schema names, such as first-name, are renamed, and the original key is kept in the property name, kept exactly as written.
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. It writes no minimum, maximum, pattern, enum or length limits; add the rules that matter by hand. 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 JSON Schema?
Paste the YAML into the input box, choose your options and press Convert to get the schema keywords. Validate documents with any validator that supports draft 2020-12, and tighten the schema by hand where the sample was too loose. The schema keywords follow the JSON form of the YAML. The conversion happens in your browser.
What does the additionalProperties option do in the schema?
Switched on, every object gets additionalProperties: false, so a document with a key the sample did not show fails validation. Leave it off for the default JSON Schema behavior, which accepts extra keys. This applies to the JSON form of your YAML.
Does the generator write limits like minLength, enum or pattern?
No. A sample cannot show the allowed ranges, so the schema has types, properties, required, items and optional formats only. Add the limits you need by hand or in your schema editor. This applies to the JSON form of your YAML.
Which JSON Schema version does the generator write?
Draft 2020-12, declared in the $schema keyword at the top. If your validator only knows an older draft, check its support for 2020-12 or edit the declaration; keywords such as type, properties, required and items also exist in older drafts. 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 JSON Schema schema keywords 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 JSON Schema schema keywords 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. It writes a JSON Schema using the draft 2020-12 dialect: a type for every value, properties for objects, items for arrays, and null, integer and number kept apart. Keys present in every object of the sample go in required (switch that off to list none), and mixed types become a type list or anyOf. The format detection option adds date-time, date, time, email, uuid, uri or ipv4 to strings that match. It cannot know ranges, patterns, enums or lengths, so none are written. Everything runs in your browser; nothing is uploaded.