Skip to main content
FormatDrop
How-To Guide

How to Convert YAML to JSON

YAML is the human-readable configuration format used in Kubernetes, Docker Compose, GitHub Actions, and Ansible. JSON is the machine-readable format used by virtually every API, config parser, and tool. Converting YAML to JSON makes configuration files importable into tools that don't speak YAML, and is a useful debugging step to flatten YAML anchors and aliases.

FormatDrop doesn't convert YAML to JSON yet. The methods below use free tools that do. FormatDrop's own converters cover common image, audio, video and PDF formats, all in your browser.

Other ways to convert YAML to JSON

  1. 1

    yq (command line)

    yq -o=json input.yaml > output.json. This is Mike Farah's Go version of yq (brew install yq); the older Python yq uses different options.

  2. 2

    Python

    python3 -c "import yaml,json,sys; print(json.dumps(yaml.safe_load(sys.stdin), indent=2, default=str))" < input.yaml > output.json. Needs pip install pyyaml. The default=str part stops dates from crashing the command. For a file with several --- documents, use yaml.safe_load_all and wrap the result in a list.

  3. 3

    Node.js

    With js-yaml installed: 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

  4. 4

    Check the result

    Comments are gone, since JSON has no comments, and anchors (&) and aliases (*) are expanded into full copies.

Why convert YAML to JSON?

YAML is written by humans configuring infrastructure; JSON is consumed by machines processing data. Converting between them is a routine operation in DevOps and API workflows — both formats describe the same structure in different syntax.

Frequently asked questions

How do I convert YAML to JSON on the command line?
Using Python: `python3 -c "import yaml,json,sys; print(json.dumps(yaml.safe_load(sys.stdin), indent=2, default=str))" < input.yaml > output.json` (default=str stops dates from crashing it). Using yq: `yq -o=json input.yaml > output.json`. Using Node.js: `node -e "const y=require('js-yaml'),fs=require('fs'); console.log(JSON.stringify(y.load(fs.readFileSync('input.yaml','utf8')),null,2))"`.
Are YAML comments preserved in the JSON output?
No — JSON has no comment syntax. All YAML comments (# ...) are stripped during conversion. If you need to keep annotations, add them as JSON keys (e.g., '_comment': 'description here') before converting.
How do YAML anchors and aliases convert to JSON?
YAML anchors (&name) and aliases (*name) are resolved to their full expanded values in the JSON output. A YAML anchor used three times becomes three identical JSON objects. This can cause JSON output to be much larger than the YAML source.
Browse FormatDrop's converters

No account. No upload. Works in any browser.