Hands-on Lab·Python Automation for IT
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
- Python3.14.4
- Control nodeUbuntu 26.04 LTS
- Managed hostsRHEL 10.0
- requests2.34.2
- paramiko5.0.0
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
- TimeAbout 18 min
- Reviewed24 August 2026
Written against the versions above. 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.
| 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 |
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