Convert YAML to JSON in JavaScript and Node.js
- js-yaml is the standard npm package for YAML parsing in Node.js.
- npm install js-yaml then yaml.load(fs.readFileSync("file.yaml", "utf8")).
- In the browser, js-yaml runs via a CDN script tag — no backend needed.
- No-code option: use the free browser converter for one-off conversions.
Table of Contents
Converting YAML to JSON in JavaScript uses the js-yaml package — the most widely adopted YAML library for both Node.js and browser environments. Install it once with npm install js-yaml, call yaml.load() on your YAML string, and you get back a JavaScript object that JSON.stringify() converts to JSON.
This guide covers Node.js file conversion, browser-side conversion with a CDN script tag, and command-line use. For developers who need a visual tool without writing code, the free browser converter at the end handles single-file conversions in seconds.
Convert YAML to JSON in Node.js Using js-yaml
Install js-yaml:
npm install js-yaml
Convert a YAML file to JSON:
const yaml = require('js-yaml');
const fs = require('fs');
const yamlContent = fs.readFileSync('input.yaml', 'utf8');
const data = yaml.load(yamlContent);
const jsonOutput = JSON.stringify(data, null, 2);
fs.writeFileSync('output.json', jsonOutput);
console.log('Converted successfully');
ES Modules syntax (Node 14+ with "type": "module" in package.json):
import yaml from 'js-yaml';
import { readFileSync, writeFileSync } from 'fs';
const data = yaml.load(readFileSync('input.yaml', 'utf8'));
writeFileSync('output.json', JSON.stringify(data, null, 2));
Quick command-line one-liner with Node:
node -e "const y=require('js-yaml'),fs=require('fs'); console.log(JSON.stringify(y.load(fs.readFileSync('input.yaml','utf8')),null,2))" > output.json
Convert YAML to JSON in the Browser Using js-yaml CDN
js-yaml runs in browser environments via a CDN script tag. No Node.js or build step required:
<script src="https://cdn.CDN/npm/[email protected]/dist/js-yaml.min.js"></script> <script> function convertYamlToJson(yamlString) { try { const data = jsyaml.load(yamlString); return JSON.stringify(data, null, 2); } catch (e) { return 'Error: ' + e.message; } } // Example usage const yaml = 'server:\n host: localhost\n port: 8080'; console.log(convertYamlToJson(yaml)); </script>
When loaded via CDN, js-yaml exposes the global jsyaml object (note: lowercase j, no hyphen). When used as an npm module, it is yaml (whatever you name the import). The API is the same either way.
YAML to JSON in TypeScript
js-yaml ships with TypeScript type definitions. Install with types:
npm install js-yaml npm install --save-dev @types/js-yaml
TypeScript example:
import * as yaml from 'js-yaml';
import * as fs from 'fs';
interface ServerConfig {
server: {
host: string;
port: number;
debug: boolean;
};
}
const raw = fs.readFileSync('config.yaml', 'utf8');
const config = yaml.load(raw) as ServerConfig;
console.log(JSON.stringify(config, null, 2));
The as ServerConfig cast gives you type safety on the output. For fully typesafe parsing with runtime validation, combine js-yaml with a schema validator like Zod — use the JSON to Zod schema generator to create the validator from a sample JSON output.
Error Handling — When yaml.load() Throws
js-yaml throws a YAMLException with a descriptive message when it encounters invalid YAML. Always wrap yaml.load() in a try/catch in production code:
const yaml = require('js-yaml');
try {
const data = yaml.load(yamlString);
return JSON.stringify(data, null, 2);
} catch (e) {
if (e instanceof yaml.YAMLException) {
console.error('YAML parse error:', e.message);
console.error('Line:', e.mark?.line + 1);
}
throw e;
}
The e.mark object contains line and column numbers pointing to the error location. Use this in your error messages to help users find the problem in their YAML.
Common causes of YAMLException: tab characters instead of spaces for indentation, unbalanced quotes, colons in strings without quoting, and duplicate keys. See the full error guide at Fix YAML to JSON Conversion Errors.
Need a Quick YAML to JSON Conversion Without Code?
Skip the npm install for one-off files. Paste YAML directly into the free browser converter and get clean JSON output instantly.
Open Free YAML to JSON ConverterFrequently Asked Questions
What npm package should I use to convert YAML to JSON in Node.js?
js-yaml is the standard choice — it has over 50 million weekly downloads, full YAML 1.2 support, TypeScript types, and active maintenance. Install with npm install js-yaml, then use yaml.load() to parse and JSON.stringify() to serialize.
Can I convert YAML to JSON in the browser without Node.js?
Yes. Include the js-yaml CDN script tag in your HTML page. It exposes the jsyaml global object. Call jsyaml.load(yamlString) to parse YAML in the browser — no server-side code needed.
What is the difference between yaml.load() and yaml.safeLoad() in js-yaml?
yaml.safeLoad() was the old API name in js-yaml v3. In js-yaml v4 (the current version), yaml.load() is safe by default — it uses the SafeLoader equivalent. The old yaml.load() with unsafe YAML support has been moved to yaml.load() with the UNSAFE_LOAD schema option. If you are on v4, just use yaml.load().
How do I handle YAML with multiple documents (--- separator) in Node.js?
Use yaml.loadAll() instead of yaml.load(). It returns an array, one element per YAML document: const docs = yaml.loadAll(multiDocYaml); docs.forEach(doc => console.log(JSON.stringify(doc)));

