JSON to YAML in Python

Copy-paste Python for both directions, plus the options that keep the output correct.

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

JSON to YAML and back with PyYAML

PyYAML plus the standard json module covers both directions. These options give the cleanest output.

json_to_yaml.py
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)
yaml_to_json.py
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.

all_documents.py
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))
ruamel_dump.py
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.

bash
# 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.

SymptomCauseFix
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.

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.