How to Convert JSON to YAML: Clean Output for Kubernetes & CI
Convert JSON to YAML the right way — pick indentation and quote style, handle multiline strings, and produce idiomatic config. Online, in Python, and on the command line.
YAML is the configuration language of modern DevOps — Kubernetes, GitHub Actions, GitLab CI, Ansible, and Docker Compose all read it. JSON, being valid YAML’s stricter cousin, converts cleanly, but the difference between a correct conversion and an idiomatic one comes down to quoting and indentation. This guide shows how to convert JSON to YAML and get output you’d actually commit.
The quickest way: convert JSON to YAML online
Copy your JSON, paste it into the JSON to YAML converter, and press Convert to YAML. You get clean, indented YAML to copy or download.
Because configs often contain secrets, prefer a client-side tool (ours runs entirely in your browser) so nothing is uploaded. It’s also ad-free and fast, with no size cap.
Use it when: you want a quick, private conversion on any device.
Quoting is the whole game
Every JSON string is quoted; idiomatic YAML quotes only when it must. The art is knowing when a string needs quotes, because YAML will otherwise misread it:
"true","false","null"— without quotes these become a boolean or null, not a string."3.14","007"— without quotes these become a number, losing the leading zero.- Values with a leading
@,&,*,:, or#, or with": "inside them.
A good converter (ours included) applies minimal quoting: clean values stay bare, risky ones get quoted. You can also force single or double quotes everywhere if your style guide requires it.
Multiline strings: use block scalars
A multiline string escaped onto one line is ugly and hard to read. Idiomatic YAML uses a literal block scalar with |:
script: |
echo "step one"
echo "step two"
This preserves line breaks and stays readable — far better than "echo \"step one\"\necho \"step two\"\n".
Pick an indentation and stick to it
YAML is whitespace-significant, so consistency matters. Two spaces is the most common; some teams use four. Choose one and apply it everywhere — mixing levels is how YAML files break. Our converter lets you select the indent width up front.
Convert JSON to YAML in Python
PyYAML handles it in two lines:
import json, yaml
data = json.load(open("data.json"))
print(yaml.safe_dump(data, sort_keys=False, default_flow_style=False))
sort_keys=False preserves your key order; default_flow_style=False forces readable block style instead of inline {}.
Convert JSON to YAML on the command line
yq is the jq-for-YAML and converts directly:
yq -p=json -o=yaml data.json
Or with Python’s one-liner, no install beyond PyYAML:
python -c 'import sys,json,yaml; print(yaml.safe_dump(json.load(sys.stdin)))' < data.json
Which method should you use?
For a quick, private conversion with control over quoting and indentation, use the online JSON to YAML converter: paste, choose options, and download. Use PyYAML when it’s part of a Python pipeline and yq when you’re in the shell. Whichever you pick, let the tool handle quoting — that’s where hand-edits go wrong.
Need the reverse? See how to convert YAML to JSON. Comparing formats? Read JSON vs XML vs YAML.
Ready to merge your JSON?
Combine files or snippets in your browser — free and private.
Open the merge toolKeep reading