How to Convert YAML to JSON in JavaScript (Node.js)
Two lines of code convert YAML to JSON, and the parser's defaults decide whether merge keys apply, dates stay strings and no means false. Here are the options that matter, tested.
Converting YAML to JSON in JavaScript takes two lines of code. The YAML to JSON Converter converts a file in the browser, and its page explains why YAML values can change type on the way. When the conversion runs in a build script or a service, the parser's defaults decide those questions for you, often without anyone noticing.
This guide shows the conversion in Node with the two common packages, yaml and js-yaml, and the options worth setting. All outputs come from running the code on one sample file.
Convert YAML to JSON in Node.js
import { readFileSync } from "node:fs";
import YAML from "yaml";
const data = YAML.parse(readFileSync("app.yaml", "utf8"));
const json = JSON.stringify(data, null, 2);With js-yaml it is load(text) instead of YAML.parse(text). js-yaml 5 has no default export, so import it as import { load } from "js-yaml". On the command line, the same package works as a one-liner:
node -e 'const YAML = require("yaml"); process.stdout.write(JSON.stringify(YAML.parse(require("fs").readFileSync(0, "utf8")), null, 2) + "\n")' < app.yamlWhat the defaults do with a tricky file
country: no
mode: 0755
released: 2026-10-18
version: 1.10
base: &base
retries: 3
service:
<<: *base
name: checkout{"country":"no","mode":755,"released":"2026-10-18","version":1.1,
"base":{"retries":3},"service":{"<<":{"retries":3},"name":"checkout"}}countrystays the stringno, because both packages follow YAML 1.2 by default.modebecomes755. YAML 1.2 writes octal as0o755, so a leading zero is just a decimal number. If the value is a file permission, it is now wrong.releasedstays a string. YAML 1.2's core rules have no date type.versionbecomes the number1.1. Quote version strings in YAML files.- The merge key
<<is not applied.servicecontains a literal key named<<instead ofretries.
Options that change the result
YAML.parse(text, { merge: true }).service
// { retries: 3, name: "checkout" }
YAML.parse(text, { version: "1.1" })
// country: false, mode: 493, released: "2026-10-18T00:00:00.000Z", merge keys appliedmerge: true applies << merge keys, which many existing YAML files rely on, Docker Compose and CI configs among them. version: "1.1" reproduces how older tools read the file: no becomes false, 0755 is octal (493), and dates become Date objects, which JSON.stringify writes as ISO strings. Use it when the file was written for a YAML 1.1 tool and you need the same meaning. Python's PyYAML also follows YAML 1.1 rules.
Files with several documents
A YAML file can hold several documents separated by ---, which Kubernetes manifests often do. YAML.parse refuses such a file with Source contains multiple documents; please use YAML.parseAllDocuments(). Convert each document and decide on the JSON shape, usually an array:
const docs = YAML.parseAllDocuments(text).map((doc) => doc.toJS());
// js-yaml: loadAll(text)
JSON.stringify(docs); // [{"name":"a"},{"name":"b"}]Duplicate keys
Both packages reject a mapping with the same key twice: yaml reports Map keys must be unique and js-yaml reports duplicated mapping key. That is stricter than JSON.parse, which keeps the last value without complaint. In a hand-edited config, the error is useful: it usually means a block was pasted twice.
Related tools
Turn JSON into clean, readable YAML for configs, pipelines and manifests.
Pretty-print JSON with configurable indentation and instant validation.
Validate JSON syntax and pinpoint the exact line and column of any error.
Parse CSV into a clean array of typed JSON objects, header-aware.
Related guides
YAML is easier to read and easier to get wrong. Here is what each format gives you for configuration, the mistakes YAML lets through, and a rule for choosing.
Two formatters can disagree about the same JSON file. Here is how a team picks one, applies it in every editor and before every commit, and makes CI enforce it.
The same file opens cleanly in VS Code and is rejected by npm. The difference is which of three JSON dialects each tool reads.
A single quoted comma breaks the split approach. Here is correct CSV parsing in Python and JavaScript, the byte order mark that renames your first column, and safe type conversion.