CertGrid CertGrid
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

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.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

Before you start

  1. The four functions, and which is which

  2. What Python types become

  3. And what has no JSON equivalent

  4. Teaching it the types you use

  5. Dictionary keys do not survive

  6. Reading a nested response without crashing on it

  7. And what it does without the default

  8. Malformed JSON, and what the exception tells you

  9. The options worth knowing

  10. One document per line, for logs and streams

  11. What domain 2 asks about this

Official sources