JSON and YAML

JSON

JSON stands for JavaScript Object Notation and is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page.

# Read JSON file: json.load() parses JSON from file object
import json
with open("filename.json", "r") as f:  # Open file in read mode
    content = json.load(f)  # Parse JSON and return Python dict/list

Write a JSON file with:

# Write JSON file: json.dump() writes Python object as JSON
import json

content = {"name": "Joe", "age": 20}
with open("filename.json", "w") as f:  # Open file in write mode
    json.dump(content, f, indent=2)  # Write JSON with 2-space indentation
Quiz

Sign in to answer this quiz and track your learning progress

Which function is used to write a Python dictionary to a JSON file?
A. json.write()
B. json.dump()
C. json.save()
D. json.export()

YAML

Compared to JSON, YAML allows a much better human maintainability and gives ability to add comments. It is a convenient choice for configuration files where a human will have to edit.

There are two main libraries allowing access to YAML files:

Install them using pip install in your virtual environment.

The first one is easier to use but the second one, Ruamel, implements much better the YAML specification, and allow for example to modify a YAML content without altering comments.

Open a YAML file with:

# Read YAML file using ruamel.yaml library
from ruamel.yaml import YAML

with open("filename.yaml") as f:
    yaml=YAML()  # Create YAML parser instance
    yaml.load(f)  # Parse YAML and return Python dict/list
Quiz

Sign in to answer this quiz and track your learning progress

What is one advantage of YAML over JSON?
A. YAML files are smaller
B. YAML is faster to parse
C. YAML allows comments and is more human-readable
D. YAML is built into Python

Anyconfig

Anyconfig is a very handy package, allowing to abstract completely the underlying configuration file format. It allows to load a Python dictionary from JSON, YAML, TOML, and so on.

Install it with:

pip install anyconfig

Usage:

# anyconfig: load configuration files in various formats (JSON, YAML, TOML, etc.)
import anyconfig
conf1 = anyconfig.load("/path/to/foo/conf.d/a.yml")  # Auto-detects format
Quiz

Sign in to answer this quiz and track your learning progress

What does the anyconfig library allow you to do?
A. Load configuration files in various formats (JSON, YAML, TOML) without specifying the format
B. Convert between different configuration formats
C. Validate configuration file syntax
D. Encrypt configuration files