Programming & Python · Lesson 10
Files, Data & JSON
Est. 30–40 min
Try it · Python in your browser
Run this lesson's example (json_log.py), change a number, run it again.
Why
Why robots must leave a paper trail
By the end you will dump/load JSON, append JSONL lines, and include units and timestamps in log fields.
Concept
The big idea: JSON and JSONL telemetry
json.dumps/json.loadsfor strings; files use dump/load.- JSON types: object, array, string, number, bool, null.
- JSONL: one object per line — easy to
tailand append. - Always store units in field names (
distance_cm) and a timestamp.
How it works
How it actually works, step by step
Design fields
ts_ms, distance_cm, cmd, battery_ok…
Pretty-print once
json.dumps(..., indent=2) for humans.
Append JSONL in the loop
One line per tick.
Validate on read
Missing keys → safe default / skip.
Visual
Visualise it: structured messages
Dict-like messages are what JSON objects become in Python.
Interactive · Data structure explorer
Ultrasonic ring buffer as a Python list
readings = [12.4,11.9,18.2,9.5,22]
latest = readings[-1] # 22.0
mid = readings[2] # 18.2
min = 9.5
max = 22.0
avg = 14.80
# robotics: rolling window for filtersCode lab
Code lab: JSON logger
#!/usr/bin/env python3
"""JSON + JSONL telemetry logger (in-memory demo)."""
import json
from io import StringIO
event = {
"ts_ms": 1500,
"distance_cm": 22.5,
"cmd": "SLOW",
"battery_ok": True,
}
print("pretty JSON:")
print(json.dumps(event, indent=2))
# JSONL = one JSON object per line (easy to append)
buf = StringIO()
for i, d in enumerate([40.0, 25.0, 12.0]):
row = {"i": i, "distance_cm": d, "cmd": "STOP" if d < 15 else "GO"}
buf.write(json.dumps(row) + "\n")
print("JSONL lines:")
print(buf.getvalue(), end="")Expected output
pretty JSON:
{
"ts_ms": 1500,
"distance_cm": 22.5,
"cmd": "SLOW",
"battery_ok": true
}
JSONL lines:
{"i": 0, "distance_cm": 40.0, "cmd": "GO"}
{"i": 1, "distance_cm": 25.0, "cmd": "GO"}
{"i": 2, "distance_cm": 12.0, "cmd": "STOP"}Real world
Where logs save you
- Flight / lab bags of JSONL for debugging.
- Config files for thresholds.
- APIs exchanging JSON with a robot host.
Mistakes
Common mistakes and confusions
Hardware & safety
Hardware lab and safety notes
Log a real run: print one JSON line per sensor reading from your script (or from an Arduino over serial) and save it to a .jsonl file you can open in any editor or load in Python.
- One line per tick:
{"ts_ms": 1500, "distance_cm": 22.5, "cmd": "SLOW"}. - Open files with
with open(path, "a") as f:so they close even on errors. - Keep logs on the laptop or Pi SD card; copy them off after the run.
Debugging
Debugging: when it doesn’t work
| Symptom | Likely cause | Fix |
|---|---|---|
json.decoder.JSONDecodeError | Half-written last line (power cut) | Skip lines that fail to parse |
TypeError: Object of type set is not JSON serializable | Sets/objects are not JSON | Convert to list / dict first |
| File is empty after the run | File never closed / flushed | Use with open(...) |
| Times jump backwards | Wall clock changed | Use time.monotonic() for durations |
Think
Think about it
Think
Why include ts_ms in every telemetry line?
Experiment
Try it in the lab: log schema
Experiment
Log schema
- Run json_log.py; confirm STOP on distance 12.
- Add battery_v to the event dict and re-print.
- Write 5 JSONL lines to a real file on your machine (open/append).
Interactive · Data structure explorer
Ultrasonic ring buffer as a Python list
readings = [12.4,11.9,18.2,9.5,22]
latest = readings[-1] # 22.0
mid = readings[2] # 18.2
min = 9.5
max = 22.0
avg = 14.80
# robotics: rolling window for filtersKnowledge check
Check your understanding
Knowledge check
1. JSONL means…
2. json.dumps turns…
3. In the demo, distance 12 produced cmd…
4. Field name distance_cm is better than d because…
5. True/false in JSON become…
Practice quiz
Coding practice
Mini assignment
Hands-on assignment
Mini assignment
Logger stub
Write a function log_event(path, event_dict) that appends one JSONL line with ts_ms.
Deliverables
- json_log_custom.py
- sample .jsonl file with ≥5 lines
Do this, step by step
- Run
json_log.pyand check the 3 JSONL lines (GO, GO, STOP). - Change the demo to write to a real file
run.jsonlwithopen(..., "a"). - Read it back line by line with
json.loads()and count STOP lines. - Write
log_event(path, event_dict)that addsts_msand appends one line; log ≥ 5 events.
Stuck? Hints, one at a time
- Hint 1: 40 → GO, 25 → GO, 12 → STOP (below 15).
- Hint 2:
ts_ms = int(time.time() * 1000). - Hint 3: Reading:
for line in open(path): row = json.loads(line).
Challenges (pick by level)
- 🟢 Easy: Pretty-print one event with
indent=2. - 🟡 Medium: Skip and count bad lines when reading.
- 🟠 Hard: Compute the average distance per command from the log.
Next step
Next, Lesson 11: Python for Robotics. Related: rosbag logging in Stage 10 — ROS 2.
Sources
Self-attested for now — keep artifacts in your lab notebook.
Complete
Today's goal
0 of 1 done today
Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).