YAML to JSON and JSON to YAML Converter
Convert YAML to JSON or JSON to YAML with the js-yaml library (version 4). Pick the direction with the tabs; parser errors show the line and column.
Related Developer Tools
Runs in your browser: your files and text are not uploaded. Free, no account needed.
Last updated: · Reviewed by the QuickTooly Team
How js-yaml reads your YAML
YAML Version and Type Coercion
js-yaml 4 follows the YAML 1.2 core schema. This changes how some plain (unquoted) values are read, compared with YAML 1.1 parsers:
yes,no,on,off: stay strings. Onlytrueandfalse(any case) become booleans.007: becomes the number 7. Leading zeros do not mean octal; write0o17for octal or quote the value to keep it as text.~,null, empty value: become JSONnull.2024-01-15: is read as a date and comes out as"2024-01-15T00:00:00.000Z". Quote it to keep the plain text..inf,.nan: becomenull, since JSON has no infinity or NaN.- Large integers beyond 253 lose precision, because they become JavaScript numbers.
Anchors, Merge Keys and Multiple Documents
- Anchors and aliases (
&base,*base) are expanded, so the JSON contains a full copy at every alias. - Merge keys
<<: *basecopy the anchored keys into the mapping, and keys written next to the merge win. - Multiple documents: input with a second
---document fails with "expected a single document in the stream, but found more". Convert one document at a time. - Duplicate keys fail with "duplicated mapping key" and the line number.
- Tabs for indentation fail with "tab characters must not be used in indentation".
Worked example
base: &b {x: 1}
child:
<<: *b
y: 2
enabled: on
zip: 007converts to:
{
"base": { "x": 1 },
"child": { "x": 1, "y": 2 },
"enabled": "on",
"zip": 7
}The tool prints nested objects across several lines; they are shown compactly here.
JSON to YAML Output
The JSON tab parses your input with JSON.parse and writes YAML with 2-space indentation and no line wrapping. Strings that YAML would read as another type are quoted: "no" becomes 'no' and "123" becomes '123'. Multi-line strings use the | block style. Key order is kept. Comments cannot be produced, since JSON has none.
YAML Converter Questions
Why did "on" stay a string instead of becoming true?
js-yaml uses the YAML 1.2 core schema, where only true and false are booleans. YAML 1.1 parsers, such as older PyYAML versions, read yes, no, on and off as booleans.
Are my YAML comments kept?
No. Comments are not part of the data, so they are lost in the JSON, and converting back to YAML cannot restore them.
Why did a value like 1.0 become 1?
YAML reads 1.0 as a number, and JSON writes the number one as 1. Quote the value if the exact text matters.
Related Tools
- JSON Compare: convert two YAML files and compare them key by key.
- JSONPath Evaluator: query the converted JSON.
- JSON to TypeScript: generate types for the config structure.