Skip to content
LocalOnly
Converters

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.

Updated October 18, 20263 min readTool runs in your browser

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.yaml

What the defaults do with a tricky file

settings.yaml
country: no
mode: 0755
released: 2026-10-18
version: 1.10
base: &base
  retries: 3
service:
  <<: *base
  name: checkout
YAML.parse(text), and js-yaml's load(text), which gave the same result
{"country":"no","mode":755,"released":"2026-10-18","version":1.1,
 "base":{"retries":3},"service":{"<<":{"retries":3},"name":"checkout"}}
  • country stays the string no, because both packages follow YAML 1.2 by default.
  • mode becomes 755. YAML 1.2 writes octal as 0o755, so a leading zero is just a decimal number. If the value is a file permission, it is now wrong.
  • released stays a string. YAML 1.2's core rules have no date type.
  • version becomes the number 1.1. Quote version strings in YAML files.
  • The merge key << is not applied. service contains a literal key named << instead of retries.

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 applied

merge: 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.

Frequently asked questions

Command Palette

Search for a tool or command