JSON to YAML in Python
Copy-paste Python for both directions, plus the options that keep the output correct.
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.
JSON to YAML and back with PyYAML
PyYAML plus the standard json module covers both directions. These options give the cleanest output.
import json
import yaml # pip install pyyaml
with open("data.json", encoding="utf-8") as f:
data = json.load(f)
with open("data.yaml", "w", encoding="utf-8") as f:
yaml.safe_dump(data, f, sort_keys=False, allow_unicode=True) import json
import yaml # pip install pyyaml
with open("data.yaml", encoding="utf-8") as f:
data = yaml.safe_load(f)
with open("data.json", "w", encoding="utf-8") as f:
# default=str turns dates and datetimes into strings
json.dump(data, f, indent=2, ensure_ascii=False, default=str) Multi-document YAML and YAML 1.2
Read every document of a Kubernetes-style file with safe_load_all, or use ruamel.yaml for YAML 1.2 rules and precise indentation.
import json
import yaml
with open("manifests.yaml", encoding="utf-8") as f:
docs = list(yaml.safe_load_all(f)) # one item per --- document
print(json.dumps(docs, indent=2, default=str)) import json
import sys
from ruamel.yaml import YAML # pip install ruamel.yaml
yaml = YAML() # round-trip mode, YAML 1.2
yaml.indent(mapping=2, sequence=4, offset=2)
with open("data.json", encoding="utf-8") as f:
yaml.dump(json.load(f), sys.stdout) Python one-liners
Handy in shell scripts and CI, as long as PyYAML is installed.
# JSON to YAML
python3 -c 'import sys, json, yaml; yaml.safe_dump(json.load(sys.stdin), sys.stdout, sort_keys=False, allow_unicode=True)' < data.json > data.yaml
# YAML to JSON
python3 -c 'import sys, json, yaml; json.dump(yaml.safe_load(sys.stdin), sys.stdout, indent=2, default=str)' < data.yaml > data.json Common problems and fixes
The surprises people hit most often when converting with PyYAML. Pick one to see it before and after the fix.
| Symptom | Cause | Fix |
|---|---|---|
| Keys come out sorted | safe_dump sorts keys by default. | Pass sort_keys=False. |
| é becomes "\xE9" | Non-ASCII characters are escaped by default. | Pass allow_unicode=True. |
| yes / no / on / off | PyYAML follows YAML 1.1, so an unquoted no loads as False. | Quote them in YAML, or use ruamel.yaml (YAML 1.2). |
| TypeError: datetime is not JSON serializable | safe_load turns 2026-10-01 into a date object. | Pass default=str to json.dump. |
| load() missing Loader | PyYAML 6 requires a Loader for yaml.load. | Use yaml.safe_load, which never runs code from the file. |
| Only the first document | safe_load reads a single document. | Use safe_load_all for --- separated files. |
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 in Python?
Load the JSON with json.load and write it with yaml.safe_dump from PyYAML: yaml.safe_dump(data, f, sort_keys=False, allow_unicode=True). The two options keep your key order and non-ASCII characters as they are.
How do I convert YAML to JSON in Python?
Read the file with yaml.safe_load and write it with json.dump(data, f, indent=2, default=str). Use yaml.safe_load_all when the file contains several documents separated by ---.
Should I use PyYAML or ruamel.yaml?
PyYAML is the most common choice and is enough for one-off conversions. ruamel.yaml implements YAML 1.2, preserves comments and key order when editing existing YAML, and gives finer control over indentation.
Why use safe_load instead of load?
yaml.load with the full loader can construct arbitrary Python objects from tags in the file, which is unsafe for untrusted input. safe_load only builds plain types: dicts, lists, strings, numbers, booleans, None and dates.
Can I convert without installing anything?
Yes. The converter at the top of this page runs in your browser and needs no Python, packages or upload. Paste JSON or YAML and copy the result.