Convert YAML to JSON in Python

Three lines with PyYAML, or zero dependencies if you only need basic cases.

With PyYAML (recommended)

Install it, then load the YAML and dump JSON:

import yaml, json

with open('data.yaml') as f:
    data = yaml.safe_load(f)

with open('data.json', 'w') as f:
    json.dump(data, f, indent=2)

yaml.safe_load parses YAML into native Python objects (dicts, lists, strings), which json.dump then serializes. Use safe_load rather than load to avoid executing arbitrary Python tags.

Without extra dependencies (basic YAML)

For simple key–value YAML you can avoid PyYAML, but you will quickly hit edge cases (nested maps, lists, type inference). For anything real, PyYAML is the pragmatic choice.

When you don't want to write code

For a one-off conversion, open the YAML to JSON converter and paste your text — it runs entirely in your browser with no upload. Need the reverse? Use the JSON to YAML converter. Not sure the input is valid? Check it with the YAML validator first.