Python JSON and Nested Data
JSON is what an API returns and what a config file increasingly is. Four functions cover it, and the interesting parts are the edges: sets and dates cannot be encoded at all, dictionary keys always come back as strings so a round trip is not necessarily equal, and reading somebody else's nested response means never assuming a key is present.
Files and Data Formats Guide 12 of 39 Beginner
- Control nodeUbuntu 26.04 LTS
- Python3.14.4
- requests2.34.2
- paramiko5.0.0
- TimeAbout 18 min
The json module is unchanged across Python 3. json.JSONDecodeError was added in 3.5 and is a subclass of ValueError, so older except ValueError: code still works.
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for API payloads and for config files that are increasingly JSON. This matters because dictionary keys do not survive the round trip - an int key goes in and a string key comes back.
- separating the four functions, and seeing what Python types become
- meeting the ordinary types with no JSON equivalent, and teaching the encoder about them
- finding that dictionary keys do not survive
- reading a nested response without crashing on the host that is missing a field
- reading a malformed document, and writing one JSON object per line
Before you start
- guide 10 -
read_textappears throughout. - guide 16 covers the key-collision behaviour this guide runs into.
-
The four functions, and which is which
-
What Python types become
-
And what has no JSON equivalent
-
Teaching it the types you use
-
Dictionary keys do not survive
-
Reading a nested response without crashing on it
-
And what it does without the default
-
Malformed JSON, and what the exception tells you
-
The options worth knowing
-
One document per line, for logs and streams
-
What domain 2 asks about this