JSON to YAML on the Command Line

Terminal one-liners for yq, Python, Node.js and PowerShell, in both directions.

Output options

JSONinput

Empty

YAMLoutput

YAML output appears here

Runs entirely in your browser, so nothing is uploaded. Fair-use limits apply (5 MB per input, rate-limited conversions) to prevent spam and bots.

Load from URL

The file is fetched directly by your browser, so the server must allow cross-origin requests.

Keyboard shortcuts

Ctrl / ⌘ + Enter Convert now
Ctrl / ⌘ + S Download the output
Alt + Shift + C Copy the output
Alt + Shift + F Fix and format the JSON input
Alt + Shift + S Swap direction
Alt + Shift + T Cycle through code, tree and map views
Ctrl / ⌘ + Z Undo, including Format, Fix and Clear
Esc Leave full screen
Editor text size

yq: the quickest way in any shell

mikefarah/yq is a single binary for Linux, macOS and Windows that reads and writes JSON and YAML.

bash · zsh · PowerShell
# JSON to YAML
yq -p json -o yaml '.' data.json > data.yaml

# YAML to JSON
yq -o json '.' data.yaml > data.json

# Multi-document YAML to one JSON array
yq ea -o json '[.]' manifests.yaml > manifests.json
Batch convert and query
# Convert every .json file in the folder
for f in *.json; do
  yq -p json -o yaml '.' "$f" > "${f%.json}.yaml"
done

# Query YAML with jq by converting on the fly
yq -o json '.' deployment.yaml | jq '.spec.template.spec.containers[].image'

PowerShell on Windows

PowerShell 7 has JSON built in; the powershell-yaml module adds YAML in both directions. Slide -Depth to see why nested data needs it.

PowerShell 7
# One-time setup
Install-Module powershell-yaml -Scope CurrentUser

# JSON to YAML
Get-Content data.json -Raw | ConvertFrom-Json -AsHashtable | ConvertTo-Yaml | Set-Content data.yaml

# YAML to JSON (raise -Depth, the default of 2 truncates nested data)
Get-Content data.yaml -Raw | ConvertFrom-Yaml | ConvertTo-Json -Depth 100 | Set-Content data.json

Node.js

js-yaml ships a CLI for YAML to JSON; the reverse is a one-line script.

bash
# YAML to JSON with the js-yaml CLI
npx js-yaml data.yaml > data.json

# JSON to YAML (npm install js-yaml first)
node -e 'const y = require("js-yaml"); process.stdout.write(y.dump(JSON.parse(require("fs").readFileSync(0, "utf8"))))' < data.json > data.yaml

Python, Ruby and kubectl

Already have one of these installed? You may not need anything else.

bash
# Python (pip install pyyaml)
python3 -c 'import sys, json, yaml; yaml.safe_dump(json.load(sys.stdin), sys.stdout, sort_keys=False)' < data.json > data.yaml

# Ruby (standard library only)
ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))' < data.json > data.yaml

# kubectl: print any manifest file as YAML or JSON
kubectl apply -f web.json --dry-run=client -o yaml

More Python options, including multi-document files and ruamel.yaml, are in the Python guide.

More tools

Converters, validators and step-by-step guides for every place JSON and YAML meet.

Frequently asked questions

How do I convert JSON to YAML on the command line?

With yq: yq -p json -o yaml '.' data.json > data.yaml. Without yq, use the Python one-liner with PyYAML, Ruby's standard library, or the powershell-yaml module on Windows.

How do I convert YAML to JSON in bash?

Run yq -o json '.' data.yaml > data.json. If the file has several documents, use yq ea -o json '[.]' file.yaml to collect them into one JSON array.

Which yq should I install?

These examples use mikefarah/yq (Go, a single binary, installable with brew, apt, snap, choco or winget). The Python package also called yq is a jq wrapper with different flags: yq -y . data.json outputs YAML and yq . data.yaml outputs JSON.

Can jq read YAML?

No, jq only reads JSON. Convert the YAML first, for example yq -o json '.' file.yaml | jq '.spec', or use yq's own jq-like expressions directly on the YAML.

Why does PowerShell ConvertTo-Json cut off my data?

ConvertTo-Json serializes only two levels deep by default and replaces anything deeper with a type name. Always pass -Depth, for example -Depth 100, when converting nested configs.