Convert JSON to YAML that reads like hand-written config
Many tools accept both JSON and YAML, but people usually maintain the YAML version: it has less punctuation, supports comments and diffs well. This JSON to YAML converter takes any valid JSON and writes it in YAML block style, which is the style you see in Kubernetes manifests, GitHub Actions workflows and Docker Compose files.
Example
[
{"id": 1, "name": "Ava", "tags": ["a", "b"]},
{"id": 2, "name": "Noah", "tags": []}
]
With a 2-space indent the result is:
- id: 1
name: Ava
tags:
- a
- b
- id: 2
name: Noah
tags: []
Objects become mappings, arrays become sequences with - items, and an empty array is written in flow style as [] because it has no items to list.
Safe quoting: why some strings get quotes
YAML decides a value's type from how it looks. A JSON string that happens to look like a number, boolean, date or null must be quoted in YAML, or it would come back as a different type. The converter handles this automatically:
{"a":"yes","b":"123","c":"","e":"key: v","f":"#x","g":null,"i":"2024-05-01"}
a: 'yes'
b: '123'
c: ''
e: 'key: v'
f: '#x'
g: null
i: '2024-05-01'
Real null stays unquoted, while the string "123" keeps its quotes. Strings that start with # or contain : are quoted so they are not read as comments or mappings.
Options
- Indent – 2 spaces (most common) or 4 spaces. With 4 spaces, list items are written as
- id: 1, keeping item content aligned with the nested keys. - Sort keys – sorts object keys alphabetically at every level. Useful for canonical files and stable diffs; leave it off to keep the order of the original JSON.
How special values are written
- Multi-line strings become literal block scalars, so
"multi\nline"is written as|-followed by two indented lines, which is much easier to read than an escaped string. - Long strings are never wrapped, so URLs and base64 values stay on one line.
- Repeated objects are written out in full; the converter does not create anchors and aliases.
What changes visually
The converted file drops JSON's braces, brackets, commas and most quotes. Structure is carried by indentation alone, so a nested object three levels deep is simply indented three steps. That is why YAML files are shorter and easier to scan, and also why indentation errors matter so much when you edit them later. If you plan to hand-edit the result, the YAML validator will catch a misplaced indent before a deployment tool does.
When to convert JSON to YAML
- Turning an API response or a
kubectl get -o jsonoutput into a manifest you can edit and commit. - Migrating a JSON config file to YAML so you can add comments.
- Producing readable fixtures or examples for documentation.
If the JSON fails to parse, fix it first with the JSON fixer. The reverse direction is covered by the YAML to JSON converter, and for TOML-based tools there is JSON to TOML.
Privacy
The conversion runs in your browser; your JSON is not uploaded. Only Load URL (fetched through our server) and Save & Share (stored for the link) involve the server.
Convert JSON to YAML in Python
Parse the JSON with the built-in json module and write it with PyYAML's yaml.safe_dump. By default PyYAML sorts keys alphabetically, so the YAML no longer follows the order of your JSON; pass sort_keys=False to keep it. Add allow_unicode=True as well, otherwise a value like café is written as "caf\xE9".
# pip install pyyaml
import json
import yaml
data = '{"name": "café", "ports": [80, 443], "db": {"host": "localhost", "ssl": true}}'
obj = json.loads(data)
text = yaml.safe_dump(obj, sort_keys=False, allow_unicode=True)
print(text)
To confirm the structure your script should produce, paste the same JSON into the converter above; the YAML validator checks the file you end up with.