JSON is a text format for structured data. Almost every REST API speaks it, and plenty of config and state files use it too. Learning to move data in and out of JSON is a daily DevOps task.
The reason it feels natural in Python is that JSON maps directly onto types you already know.
A JSON document is built from a small set of pieces:
{}, becomes a Python dict.[], becomes a Python list.str.int or float.true and false become True and False.null becomes None.Here is a small JSON document:
{
"service": "api",
"port": 8080,
"enabled": true,
"tags": ["web", "public"]
}
Read into Python, that is a dict with four keys. data["port"] is the
integer 8080, data["enabled"] is True, and data["tags"] is a
list of two strings.
Because JSON becomes ordinary dicts and lists, you work with it using
everything you already know: indexing, .get(), loops, comprehensions.
There is no special JSON object to learn. You parse the text once, then
it is just Python data.
The two nodes that follow show the two directions: turning JSON text into Python objects, and turning Python objects back into JSON text.
true/false, and null map to str, int or
float, True/False, and None.