JSON to YAML on the Command Line
Terminal one-liners for yq, Python, Node.js and PowerShell, in both directions.
JSONinput
YAMLoutput
Each line of the output, in default formatting, next to the path of the value it holds. Click a row to copy the path.
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.
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.
# 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 # 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.
# 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.
# 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.
# 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.
Converters
Format & validate
Convert by use case
- Kubernetes Turn kubectl JSON output and API objects into clean, apply-ready manifests.
- Docker Compose Build compose.yaml from JSON, including ports, environment and volumes.
- GitHub Actions Write workflow files from JSON without the on: / true trap.
- OpenAPI / Swagger Convert OpenAPI 3 and Swagger 2.0 specs between JSON and YAML.
- CloudFormation Convert AWS CloudFormation templates from JSON to YAML, intrinsic functions included.
Code & command-line guides
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.