Convert YAML to JSON in Python (3 Lines) — or Skip the Code

Need YAML as JSON inside a Python script — or just need the output once? Here's the three-line version, the edge cases, and the no-code shortcut.

The 3-line conversion

pip install pyyaml, then: import yaml, json; data = yaml.safe_load(open('config.yaml')); print(json.dumps(data, indent=2)). Always use safe_load — plain load can execute arbitrary Python tags from untrusted files.

Multiple documents in one file

YAML files can contain several documents separated by ---. Use yaml.safe_load_all() and wrap the result in a list: json.dumps(list(yaml.safe_load_all(f))). This is the step most snippets skip — and why Kubernetes manifests often break naive converters.

Type gotchas to expect

YAML 1.1 quirks carry into the load: unquoted 'no' becomes false, '3:30' can become 12600 (sexagesimal), and leading-zero numbers may surprise you. If a value looks wrong in the JSON, quote it in the source YAML.

When you don't need Python at all

For a one-off conversion, the browser tool on this site does the same thing instantly — client-side, both directions, multi-document support included. Paste, convert, copy. No environment, no install.

Convert your YAML file now

Free — upload a PDF or image and download a clean spreadsheet.

Open the converter →