X Xerobit

YAML to JSON in Python — Convert YAML Files with PyYAML and ruamel.yaml

Python makes YAML-to-JSON conversion easy with PyYAML and ruamel.yaml. Here's how to parse YAML files, convert to JSON, handle edge cases, and convert entire directories of...

Mian Ali Khalid · · 4 min read
Use the tool
YAML ↔ JSON Converter
Convert between YAML and JSON formats with full fidelity.
Open YAML ↔ JSON Converter →

Python has two major YAML libraries: PyYAML (simpler, widely used) and ruamel.yaml (preserves comments and formatting). Both convert YAML to Python dicts that you can then serialize to JSON.

Use the YAML to JSON Converter to convert online without writing code.

Basic YAML to JSON with PyYAML

pip install pyyaml
import yaml
import json

# From string:
yaml_string = """
name: Alice
age: 30
address:
  city: London
  zip: SW1A 1AA
tags:
  - developer
  - python
"""

data = yaml.safe_load(yaml_string)
json_output = json.dumps(data, indent=2)
print(json_output)

Output:

{
  "name": "Alice",
  "age": 30,
  "address": {
    "city": "London",
    "zip": "SW1A 1AA"
  },
  "tags": [
    "developer",
    "python"
  ]
}

Reading from a file

import yaml
import json
from pathlib import Path

def yaml_to_json(yaml_path: str, json_path: str = None, indent: int = 2) -> str:
    with open(yaml_path, 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f)
    
    json_str = json.dumps(data, indent=indent, ensure_ascii=False)
    
    if json_path:
        with open(json_path, 'w', encoding='utf-8') as f:
            f.write(json_str)
    
    return json_str

# Usage:
json_output = yaml_to_json('config.yaml', 'config.json')
print(json_output)

safe_load vs load

Always use yaml.safe_load() for untrusted input:

# SAFE: only parses basic YAML types
data = yaml.safe_load(yaml_string)

# UNSAFE: can execute arbitrary Python (!!python/object attacks)
data = yaml.load(yaml_string, Loader=yaml.FullLoader)

# For multiple documents in one file:
documents = list(yaml.safe_load_all(yaml_string))

Multi-document YAML files

YAML files can contain multiple documents separated by ---:

yaml_multi = """
---
name: config-v1
value: 1

---
name: config-v2
value: 2
"""

# Parse all documents:
documents = list(yaml.safe_load_all(yaml_multi))
json_output = json.dumps(documents, indent=2)
# [{"name": "config-v1", "value": 1}, {"name": "config-v2", "value": 2}]

Handling YAML types not in JSON

YAML has types JSON doesn’t:

import yaml
import json
from datetime import datetime, date

yaml_string = """
created_at: 2026-05-11
updated_at: 2026-05-11T10:00:00
value: !!python/bytes 'hello'
"""

data = yaml.safe_load(yaml_string)
# data['created_at'] is a datetime.date object
# data['updated_at'] is a datetime.datetime object

# JSON doesn't support dates natively — use a custom serializer:
def json_serializer(obj):
    if isinstance(obj, (datetime, date)):
        return obj.isoformat()
    raise TypeError(f"Object of type {type(obj)} is not JSON serializable")

json_output = json.dumps(data, default=json_serializer, indent=2)

Converting a directory of YAML files

import yaml
import json
from pathlib import Path

def convert_yaml_directory(input_dir: str, output_dir: str):
    input_path = Path(input_dir)
    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)
    
    converted = 0
    errors = []
    
    for yaml_file in input_path.glob('**/*.{yaml,yml}'):
        relative = yaml_file.relative_to(input_path)
        json_file = output_path / relative.with_suffix('.json')
        json_file.parent.mkdir(parents=True, exist_ok=True)
        
        try:
            with open(yaml_file, 'r') as f:
                data = yaml.safe_load(f)
            
            with open(json_file, 'w') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
            
            converted += 1
        except Exception as e:
            errors.append((yaml_file, str(e)))
    
    print(f"Converted {converted} files")
    if errors:
        for path, error in errors:
            print(f"Error {path}: {error}")

convert_yaml_directory('./config/yaml', './config/json')

Using ruamel.yaml (comment-preserving)

ruamel.yaml preserves comments, round-trips formatting, and handles YAML 1.2:

pip install ruamel.yaml
from ruamel.yaml import YAML
import json

yaml = YAML()
yaml.preserve_quotes = True

with open('config.yaml', 'r') as f:
    data = yaml.load(f)

# Convert to JSON (ruamel uses CommentedMap, need to convert):
import json
from ruamel.yaml.comments import CommentedMap, CommentedSeq

def to_dict(obj):
    if isinstance(obj, CommentedMap):
        return {k: to_dict(v) for k, v in obj.items()}
    if isinstance(obj, CommentedSeq):
        return [to_dict(v) for v in obj]
    return obj

json_output = json.dumps(to_dict(data), indent=2)

CLI one-liner

# Convert YAML to JSON in one line:
python -c "import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))" < config.yaml

# Or as a script:
cat config.yaml | python3 -c "
import sys, yaml, json
data = yaml.safe_load(sys.stdin)
print(json.dumps(data, indent=2))
"

Related posts

Related tool

YAML ↔ JSON Converter

Convert between YAML and JSON formats with full fidelity.

Written by Mian Ali Khalid. Part of the Data & Format pillar.