Convert JSON to YAML from Command Line [CLI Guide]

Convert JSON to YAML from the command line using yq, Python, or Ruby. The easiest method is yq: yq -P file.json > file.yaml. For quick one-liners without installing tools, use Python's built-in modules or Ruby's YAML library. Need help understanding the difference between JSON and YAML? See our YAML vs JSON comparison. This bash JSON to YAML guide covers Linux, macOS, and Windows.

Method 1: Using yq (Recommended)

yq is the best CLI tool for JSON to YAML conversion. It's fast, handles streaming, and supports complex queries similar to jq.

Install yq

# macOS (Homebrew)
brew install yq

# Ubuntu/Debian
sudo apt-get install yq

# Or download binary directly
sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq
sudo chmod +x /usr/bin/yq

# Windows (Chocolatey)
choco install yq

# Verify installation
yq --version

yq Convert JSON to YAML

# Convert JSON file to YAML
yq -P input.json > output.yaml

# Convert and print to stdout
yq -P input.json

# Convert from stdin
cat input.json | yq -P

# With pretty output (default)
yq -P --indent 2 input.json > output.yaml
Note: The -P flag means "pretty print" which outputs block-style YAML instead of flow style.

Method 2: Using Python (No Install Required)

Python is often pre-installed on Linux and macOS. Use this one-liner to convert JSON to YAML from the command line. For more comprehensive Python examples, see our Python JSON to YAML guide.

# JSON to YAML (one-liner)
python -c "import json,yaml,sys; print(yaml.dump(json.load(sys.stdin)))" < input.json > output.yaml

# More readable version with safe_dump
python3 -c "
import json, yaml, sys
data = json.load(sys.stdin)
print(yaml.safe_dump(data, default_flow_style=False))
" < input.json > output.yaml

# YAML to JSON (reverse)
python -c "import json,yaml,sys; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))" < input.yaml > output.json
Prerequisite: Python needs PyYAML installed. Run pip install pyyaml if you get an import error.

Method 3: Using Ruby

Ruby has YAML and JSON libraries built-in, making it perfect for quick conversions without installing additional packages:

# JSON to YAML (Ruby one-liner)
ruby -ryaml -rjson -e "puts YAML.dump(JSON.parse(STDIN.read))" < input.json > output.yaml

# YAML to JSON
ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.load(STDIN.read))" < input.yaml > output.json

Method 4: Using jq + yq

jq is excellent for JSON transformations but cannot output YAML. Combine it with yq for powerful pipelines:

# Basic jq to yq pipeline
cat input.json | jq '.' | yq -P > output.yaml

# Transform JSON, then convert to YAML
cat input.json | jq '.items[] | select(.active)' | yq -P > filtered.yaml

# Extract specific fields and convert
jq '{name: .name, version: .version}' package.json | yq -P

Tool Comparison Table

Tool Install Required Speed Best For Platform
yq Yes Fast Daily use, scripts Linux, macOS, Windows
Python Usually no* Medium Quick conversion Linux, macOS, Windows
Ruby Usually no Medium One-off tasks Linux, macOS
jq + yq Yes (both) Fast Complex transforms Linux, macOS, Windows

* Python needs PyYAML: pip install pyyaml

Convert Multiple Files

Convert all JSON files in a directory to YAML using a bash loop. This is especially useful for managing configuration files or DevOps workflows:

# Batch convert all JSON files to YAML (Linux/macOS)
for f in *.json; do
  yq -P "$f" > "${f%.json}.yaml"
  echo "Converted: $f -> ${f%.json}.yaml"
done

# Using find for recursive conversion
find . -name "*.json" -exec sh -c 'yq -P "$1" > "${1%.json}.yaml"' _ {} \;

# Windows PowerShell
Get-ChildItem *.json | ForEach-Object {
  yq -P $_.FullName > ($_.BaseName + ".yaml")
}

YAML to JSON CLI

Convert YAML back to JSON using the same tools:

# yq: YAML to JSON
yq -o json input.yaml > output.json

# Python: YAML to JSON
python -c "import json,yaml,sys; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))" < input.yaml > output.json

# Ruby: YAML to JSON
ruby -ryaml -rjson -e "puts JSON.pretty_generate(YAML.load(STDIN.read))" < input.yaml > output.json

Frequently Asked Questions

What is the best CLI tool to convert JSON to YAML?

yq is the best CLI tool for JSON to YAML conversion. It's fast, supports streaming, and can handle complex transformations. Install with brew install yq (macOS) or apt-get install yq (Ubuntu). The basic command is: yq -P input.json > output.yaml

How do I convert JSON to YAML in bash without installing anything?

Use Python (usually pre-installed): python -c "import json,yaml,sys; print(yaml.dump(json.load(sys.stdin)))" < input.json. Note: Python needs PyYAML installed. For truly zero-install, use Ruby: ruby -ryaml -rjson -e "puts YAML.dump(JSON.parse(STDIN.read))" < input.json

How do I convert JSON to YAML on Linux?

On Linux, install yq with sudo apt-get install yq or sudo snap install yq. Then run yq -P file.json > file.yaml. For batch conversion of multiple files: for f in *.json; do yq -P "$f" > "${f%.json}.yaml"; done

Can I use jq to convert JSON to YAML?

jq alone cannot convert to YAML—it only outputs JSON. However, you can pipe jq output to yq: cat input.json | jq '.' | yq -P > output.yaml. This is useful when you need to transform JSON before converting to YAML.

Need to convert JSON to YAML quickly?

Use our free online converter—no installation required.

Open Converter Tool →