How to Convert JSON to YAML in Python

JSON (JavaScript Object Notation) and YAML (YAML Ain’t Markup Language) are both human-readable data formats, but YAML is often preferred for its simplicity and readability.

In this tutorial, you’ll learn how to convert JSON to YAML using different Python libraries.

 

 

Using PyYAML

To convert JSON to YAML, you can use the PyYAML library. First, install it using pip if you haven’t already:

pip install pyyaml

You can use the yaml.dump function to convert JSON data to YAML format like this:

import json
import yaml
json_data = '''
{
    "name": "Ahmed",
    "age": 30,
    "city": "Cairo",
    "skills": ["Python", "Data Analysis", "Machine Learning"]
}
'''
data = json.loads(json_data)
yaml_data = yaml.dump(data, default_flow_style=False)
print(yaml_data)

Output:

age: 30
city: Cairo
name: Ahmed
skills:
- Python
- Data Analysis
- Machine Learning

The JSON data is converted into a YAML format, which is more readable.

The default_flow_style=False argument ensures that the YAML output is in block style, which is human-friendly.

 

Using PyYAML safe_dump

For a safer conversion, use safe_dump to avoid executing arbitrary code during the conversion process.

import json
import yaml
json_data = '''
{
    "name": "Fatma",
    "age": 25,
    "city": "Alexandria",
    "skills": ["JavaScript", "React", "Node.js"]
}
'''
data = json.loads(json_data)
yaml_data = yaml.safe_dump(data, default_flow_style=False)
print(yaml_data)

Output:

age: 25
city: Alexandria
name: Fatma
skills:
- JavaScript
- React
- Node.js

 

Using the ruamel.yaml

The ruamel.yaml library offers more control over YAML formatting. Install it using pip:

pip install ruamel.yaml

You can use ruamel.yaml to convert JSON to YAML with enhanced features:

import json
from ruamel.yaml import YAML
from io import StringIO
json_data = '''
{
  "name": "Hassan",
  "age": 40,
  "city": "Giza",
  "skills": ["Java", "Spring", "Hibernate"]
}
'''
data = json.loads(json_data)
yaml = YAML()
stream = StringIO()
yaml.dump(data, stream)
yaml_data = stream.getvalue()
print(yaml_data)

Output:

name: Hassan
age: 40
city: Giza
skills:
- Java
- Spring
- Hibernate

 

Benchmark Test

To compare the performance of these methods, you can run a simple benchmark test.

import json
import yaml
from ruamel.yaml import YAML
import timeit
from io import StringIO
json_data = '''
{
"name": "Youssef",
"age": 28,
"city": "Aswan",
"skills": ["Go", "Docker", "Kubernetes"]
}
'''
data = json.loads(json_data)
multiplied_data = [data] * 1000
def benchmark_pyyaml():
  yaml.dump(multiplied_data, default_flow_style=False)
def benchmark_safe_dump():
  yaml.safe_dump(multiplied_data, default_flow_style=False)
def benchmark_ruamel_yaml():
  yaml = YAML()
  stream = StringIO()
  yaml.dump(multiplied_data, stream)
pyyaml_time = timeit.timeit(benchmark_pyyaml, number=1000)
safe_dump_time = timeit.timeit(benchmark_safe_dump, number=1000)
ruamel_yaml_time = timeit.timeit(benchmark_ruamel_yaml, number=1000)
print(f"PyYAML dump time: {pyyaml_time:.6f} seconds")
print(f"PyYAML safe_dump time: {safe_dump_time:.6f} seconds")
print(f"ruamel.yaml dump time: {ruamel_yaml_time:.6f} seconds")

Output:

PyYAML dump time: 4.178811 seconds
PyYAML safe_dump time: 4.155356 seconds
ruamel.yaml dump time: 17.931877 seconds

The benchmark results show the time taken by each method to convert JSON to YAML 1000 times.

PyYAML is much faster in converting json to YAML.

Leave a Reply

Your email address will not be published. Required fields are marked *