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
Sign in to answer this quiz and track your learning progress
json.write()json.dump()json.save()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
Sign in to answer this quiz and track your learning progress
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
Sign in to answer this quiz and track your learning progress