To convert YAML to JSON in JavaScript, parse the YAML into a plain object with a library such as js-yaml or yaml, then pass that object to JSON.stringify(). JavaScript can write JSON on its own, but it has no YAML parser built in, so the library is the only extra piece. The whole job is two lines once the package is installed.
Which library should you use for YAML to JSON in JavaScript?
Both js-yaml and yaml do the job, and either is a fine default. The js-yaml README describes it as a "YAML 1.2 parser and serializer for JavaScript". The yaml package docs cover yaml@2, which has no external dependencies and runs on Node.js and modern browsers.
| js-yaml | yaml | |
|---|---|---|
| Install | npm install js-yaml |
npm install yaml |
| Parse one document | load(text) |
parse(text) |
| Several documents | loadAll(text) |
parseAllDocuments(text) |
| Error position | Line and column in the message | YAMLParseError with linePos |
| Unquoted dates | Date in 4.1.0, string in 5.x | String |
My take: if one is already in node_modules, keep it. Starting fresh, use yaml. Its errors point at the exact spot and its defaults match the YAML 1.2 spec.
Convert a YAML string with js-yaml
With js-yaml, call load() on the text and hand the result to JSON.stringify().
import { load } from 'js-yaml';
const text = 'name: api\nport: 8080\n';
const data = load(text);
const json = JSON.stringify(data, null, 2);
The third argument to JSON.stringify sets the indent. MDN notes the number is clamped to 10, so 2 gives the usual two-space output. Drop it for minified JSON.
Convert a YAML string with the yaml package
The yaml package works the same way, with parse() in place of load().
import { parse } from 'yaml';
const json = JSON.stringify(parse(text), null, 2);
The docs say YAML.parse throws on error and only accepts a single document. If you'd rather collect errors than catch them, parseDocument() returns a document object with errors and warnings arrays, and doc.toJSON() gives you only JSON values.
Read a .yaml file and write a .json file in Node
In Node, read the file as UTF-8 text, parse it, and write the string back out.
import { readFile, writeFile } from 'node:fs/promises';
import { load } from 'js-yaml';
const file = 'config.yaml';
const text = await readFile(file, 'utf8');
const data = load(text, { filename: file });
await writeFile('config.json', JSON.stringify(data, null, 2));
The js-yaml usage guide says passing it makes parse errors name the input source, which helps when a script loops over dozens of files. .yml files need no different handling; see .yaml vs .yml.
Multi-document YAML files
A file with --- separators holds several documents, and load() and parse() won't read it. Use loadAll() in js-yaml, which returns an array and accepts empty streams. In yaml, use parseAllDocuments() and call toJSON() on each result. Kubernetes manifests are the usual place you'll hit this.
The same YAML can give different JSON
The JSON you get depends on the library and its version, mainly for dates and empty files. I ran this input through three versions on 8 November 2026:
name: api
released: 2024-03-01
country: NO
port: 8080
- js-yaml 4.1.0 gave
"released":"2024-03-01T00:00:00.000Z". It reads the date as a JavaScriptDate, andJSON.stringifywrites a Date withtoISOString(). - js-yaml 5.5.0 gave
"released":"2024-03-01". - yaml 2.9.1 also gave
"released":"2024-03-01".
The cause is in the js-yaml changelog. Version 5.0.0 switched load to CORE_SCHEMA by default, which has no timestamp type. The same release made load() throw on empty input instead of returning undefined. In my test, 5.5.0 threw "expected a document, but the input is empty".
All three kept country: NO as the string "NO". The yaml docs show that parse('No', { version: '1.1' }) returns false, so only opt into YAML 1.1 rules if you need them. For why dates are messy in JSON in the first place, see JSON date format.
Quote values you want to stay as text, like released: "2024-03-01". Then every parser agrees.
Errors you'll actually see
Both libraries throw on invalid YAML, so wrap the call in try/catch and show the message. These came straight from my test runs:
- Duplicate key, js-yaml:
duplicated mapping key (2:1) - Duplicate key, yaml:
Map keys must be unique at line 2, column 1 - Tab in indentation, js-yaml:
tab characters must not be used in indentation (2:1) a: b: cin yaml:Nested mappings are not allowed in compact mappings at line 1, column 4
In yaml, the thrown error is a YAMLParseError with a linePos array, useful for underlining the bad spot in an editor.
What JSON.stringify drops or changes
YAML can hold values that JSON can't, and JSON.stringify quietly rewrites them. Per MDN:
NaNandInfinitybecomenull. YAML's.nanand.infend up there too; more in NaN and Infinity in JSON.undefined, functions and Symbols vanish from objects.- A
BigIntthrows aTypeError. - Circular references throw a
TypeError. YAML anchors and aliases can create shared references, but a normal parse gives plain copies.
Big integers are another trap. In my test, yaml parsed big: 12345678901234567890 and the JSON came out as 12345678901234567000. The value became a JavaScript number first. Large numbers in JSON.parse explains why.
Converting in the browser
Both libraries run client-side, so you can convert YAML without a server. Import them through your bundler as above. js-yaml 5 moved its browser builds to the js-yaml/browser export, per the changelog.
The limitation: neither library checks structure, only syntax. A YAML file can parse cleanly and still be missing a required key. If you need that check, validate the JSON afterwards, as in validating JSON against a JSON Schema. And when you need to go the other way, the JSON to YAML converter runs in your browser.
FAQ
Does JavaScript have a built-in YAML parser?
No. JavaScript ships JSON.parse and JSON.stringify, but YAML needs a library. The two common ones are js-yaml and yaml, and both work in Node.js and in the browser.
Is js-yaml load() safe for untrusted input?
Since js-yaml 4, load, loadAll and dump are safe by default, and the old safeLoad was removed in version 5. You still want size limits and error handling around input you don't control.
How do I convert a YAML file with several documents?
Use loadAll() in js-yaml or parseAllDocuments() in yaml. Both give you one value per document, so you can stringify the whole array or each item on its own.
Why did my YAML date turn into a timestamp in the JSON?
Older js-yaml versions (4.1.0, for example) turn an unquoted date into a JavaScript Date, and JSON.stringify writes a Date as an ISO string. js-yaml 5.x and the yaml package keep it as a plain string. Quote the date to get the same result everywhere.