Need to convert YAML to JSON in Node.js for configs, scripts, or API payloads? This guide will show how to switch between YAML and JSON in minutes using two popular packages: js-yaml and yaml. You’ll find simple examples, easy-to-understand explanations, and practical steps that you can use in real projects, such as Kubernetes configs, CI/CD pipelines, or app settings.
Project Setup and Package Installation
For better understanding and keeping code separate, let’s create new application just for this example. You can use any existing Node.js application. Create a project folder and initialize npm and install both js-yaml yaml libraries.
mkdir yaml-json-demo
cd yaml-json-demo
npm init -y
npm install js-yaml yamlLet’s create a sample config file to that you will use for converting to JSON and back.
app: shop
port: 4000
debug: false
features:
- login
- payments
- reportsIt provides basic setup. Lets use both library as your Node.js YAML converter with minimal changes.
Converting YAML to JSON in Node.js
Let’s use both packages to convert YAML to JSON in Node.js. You can choose any of this both libraries in you actual application.
Converting YAML to JSON using js-yaml
This nodejs yaml parser example reads config.yml and outputs config.json.
const fs = require('fs');
const yaml = require('js-yaml');
const input = fs.readFileSync('config.yml', 'utf8');
const data = yaml.load(input);
fs.writeFileSync('config.json', JSON.stringify(data, null, 2));To run this script enter below command into terminal:
node yaml-to-json-jsyaml.jsOnce code runs, you can see output like below in config.json file.
{
"app": "shop",
"port": 4000,
"debug": false,
"features": ["login", "payments", "reports"]
}Using yaml to Convert YAML
If you prefer a modern API or approach, try the yaml package for a quick yaml to json nodejs workflow. It’s process details faster easily.
const fs = require('fs');
const YAML = require('yaml');
const input = fs.readFileSync('config.yml', 'utf8');
const data = YAML.parse(input);
fs.writeFileSync('config.json', JSON.stringify(data, null, 2));This type of real life approach can be used to store feature toggles in YAML but send JSON to front-end dashboards. These scripts bridge that quickly.
Converting JSON to YAML in Node.js
You can also convert json to yaml in nodejs when you need human-friendly config files or cleaner diffs in Git. First, create a sample JSON file. For these example we will use our config file.
Using js-yaml to Convert YAML
Let’s take an example for reverse conversion using js-yaml library. We will use that created JSON config file and convert it the YAML data.
const fs = require('fs');
const yaml = require('js-yaml');
const input = fs.readFileSync('config.json', 'utf8');
const data = JSON.parse(input);
const yml = yaml.dump(data);
fs.writeFileSync('config.yml', yml);Here, It will read JSON file and parse it and then use this package to dump data to the file.
Using yaml
Below is the script to perform same operation using this yaml package.
const fs = require('fs');
const YAML = require('yaml');
const input = fs.readFileSync('config.json', 'utf8');
const data = JSON.parse(input);
const yml = YAML.stringify(data);
fs.writeFileSync('config.yml', yml);You can use these scripts as per your requirement and it will work for your projects.
Conclusion
You just saw how to Convert YAML to JSON in Node.js, using js-yaml and yaml to flip JSON back to YAML. Both options are fast, reliable, and easy to maintain. Use these nodejs yaml converter tools for configs, API payloads, or automation scripts to keep your data flowing smoothly across formats. You can checkout our post on Read and Write Excel Files in Node.js Using XLSX.

