Stage 02 · Lesson 10/15

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.loads for strings; files use dump/load.
  • JSON types: object, array, string, number, bool, null.
  • JSONL: one object per line — easy to tail and append.
  • Always store units in field names (distance_cm) and a timestamp.

How it works

How it actually works, step by step

  1. Design fields

    ts_ms, distance_cm, cmd, battery_ok…

  2. Pretty-print once

    json.dumps(..., indent=2) for humans.

  3. Append JSONL in the loop

    One line per tick.

  4. 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 filters

Code lab

Code lab: JSON logger

json_log.py · Python
#!/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
SymptomLikely causeFix
json.decoder.JSONDecodeErrorHalf-written last line (power cut)Skip lines that fail to parse
TypeError: Object of type set is not JSON serializableSets/objects are not JSONConvert to list / dict first
File is empty after the runFile never closed / flushedUse with open(...)
Times jump backwardsWall clock changedUse time.monotonic() for durations
NoteRead the last line of a Python error first — it names the problem — then the line number above it. Change one thing at a time and re-run.

Think

Think about it

Think

Why include ts_ms in every telemetry line?

Experiment

Try it in the lab: log schema

Experiment

Log schema

  1. Run json_log.py; confirm STOP on distance 12.
  2. Add battery_v to the event dict and re-print.
  3. 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 filters

Knowledge 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

Loading practice quiz…

Coding practice

Loading coding exercises…

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

  1. Run json_log.py and check the 3 JSONL lines (GO, GO, STOP).
  2. Change the demo to write to a real file run.jsonl with open(..., "a").
  3. Read it back line by line with json.loads() and count STOP lines.
  4. Write log_event(path, event_dict) that adds ts_ms and appends one line; log ≥ 5 events.

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: 40 → GO, 25 → GO, 12 → STOP (below 15).
  2. Hint 2: ts_ms = int(time.time() * 1000).
  3. 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

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in