Structured messages (JSON-shaped dicts)

IoT devices and web APIs speak JSON: nested dictionaries and lists. Even before import json, thinking in keys and lists is the skill. A telemetry packet is not a paragraph — it is a tree you walk with the same for and ["key"] you already have.

{ }[ ]

Telemetry packet

Nested keys like an API payload.

You will be able to

  • Read nested keys
  • Loop a list inside a record

Payloads are trees

packet["readings"] is a list. packet["device"] is a string. Walk with the tools you have. When you later call json.loads(text) in full Python, you get this exact structure.

Pretty-printing JSON is for humans. Programs should navigate keys, not slice characters of the raw text.

APIs and files speak this

A weather HTTP response, a WhatsApp export converted by a tool, a settings.json next to your app — all become dicts and lists. Advanced level will use requests.get(...).json() and get this tree. You already know how to walk it.

When a key is missing, decide: error (required field) or a default. Silent packet["temp"] that sometimes does not exist is how kiosks show blank screens.

Words that matter

JSON
A text format for nested objects and arrays; maps to dicts and lists in Python.

Common mistakes

Avoid: Parsing JSON with replace and split on commas.

Do this: Use the json module in real Python; here, use dicts directly.

Example program — One telemetry packet

Print each reading.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Campus node

A packet with device gate-1 must print gate-1.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. JSON objects map cleanly to Python…

Progress is stored in a browser cookie on this device.