YAML to JSON Conversion: All Methods Explained

Converting YAML to JSON is a routine task across DevOps, backend development, and data engineering. YAML config files need to become JSON payloads for API calls. Kubernetes manifests need to convert for programmatic processing. GitHub Actions workflows need their data extracted into JSON for reporting tools. This guide covers every method — online converter, Python, Node.js, and command line.

The Fastest Method: Convert YAML to JSON Online

For quick one-off conversions, Format Pilot’s free online converter is the fastest option. Open it in any browser, paste your YAML, select JSON as the output format, and click Convert. The result is formatted JSON you can copy or download immediately. No installation, no account, and your data never leaves your browser.

Convert YAML to JSON Using Python

Python is the most common language for YAML-to-JSON conversion in scripts and automation pipelines. Install pyyaml if you haven’t already:

pip install pyyaml

Basic conversion script:

import yaml
import json
import sys

def yaml_to_json(yaml_file, json_file=None):
    with open(yaml_file, 'r') as f:
        data = yaml.safe_load(f)
    
    json_str = json.dumps(data, indent=2, default=str)
    
    if json_file:
        with open(json_file, 'w') as f:
            f.write(json_str)
    else:
        print(json_str)

yaml_to_json('config.yaml', 'config.json')

Why use yaml.safe_load() instead of yaml.load()?

Always use yaml.safe_load() rather than yaml.load() for converting untrusted or external YAML files. The load() function can execute arbitrary Python code embedded in YAML via special tags — a significant security risk. safe_load() only processes standard YAML types and raises an error if it encounters unsafe constructs.

Convert YAML to JSON Using Node.js

npm install js-yaml
const yaml = require('js-yaml');
const fs   = require('fs');

const yamlContent = fs.readFileSync('config.yaml', 'utf8');
const data = yaml.load(yamlContent);
const jsonOutput = JSON.stringify(data, null, 2);

fs.writeFileSync('config.json', jsonOutput);
console.log('Conversion complete.');

Convert YAML to JSON via Command Line

On systems with Python installed (most Linux and Mac machines), you can convert YAML to JSON directly from the terminal without writing a script:

python3 -c "import sys,yaml,json; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))" < input.yaml > output.json

If you have yq installed (a YAML command-line processor):

yq -o=json input.yaml > output.json

Handling Common YAML-to-JSON Conversion Issues

Boolean values behave differently between YAML and JSON. In YAML, yes, no, on, and off are parsed as booleans in YAML 1.1. In YAML 1.2 (and JSON), only true and false are booleans. If your YAML file uses yes/no, PyYAML may convert them to true/false in JSON output — which is usually correct, but worth verifying.

Numeric strings like 0800 (phone numbers, postal codes) may be parsed as octal integers by YAML parsers. Quote these values in your YAML source to ensure they remain strings after conversion.

Frequently Asked Questions

Can I convert YAML to JSON without installing anything?

Yes. Use Format Pilot’s free online converter — paste your YAML and get JSON instantly in any browser with no software, extensions, or accounts required.

Does YAML support all JSON data types?

Yes — every valid JSON file is also valid YAML. All JSON types (string, number, boolean, null, array, object) map directly to YAML equivalents. Conversion from YAML to JSON is lossless for standard types, except that YAML comments are dropped since JSON has no comment syntax.

What is the best Python library for YAML to JSON conversion?

PyYAML is the standard library for YAML parsing in Python. For YAML 1.2 compliance (which aligns more closely with JSON semantics), ruamel.yaml is a better choice — it handles edge cases like boolean aliases and numeric string parsing more predictably than PyYAML.