Skip to content

Working with JSON and CSV

Beginning of the lesson

Working with JSON and CSV

Turn Python objects into files other programs can read — and read them back

Big Idea: A running program keeps its data as live objects in memory — dicts, lists, numbers. The moment the program stops, that memory is gone. Serialization is the act of turning those objects into plain text you can save to a file or send over a network, and deserialization is loading it back. Python ships with two batteries-included modules for the two most common text formats: json for nested, typed data and csv for flat, table-shaped data.
By the end of this lesson you will be able to:
  • explain what serialization is and why files outlive your program's memory,
  • convert between Python objects and JSON text with json.dumps/loads and read/write files with json.dump/load,
  • read and predict the Python ↔ JSON type mapping (and its sharp edges: tuples, None, dict keys),
  • read and write tabular data with csv.reader/writer and the friendlier csv.DictReader/DictWriter,
  • avoid the classic CSV traps — the blank-row bug, everything-is-a-string, and encoding,
  • choose JSON vs CSV for a given job on purpose, not by habit.

1. Structured data and serialization

Think of your program's data like the contents of a room: furniture, boxes, cables — everything arranged and connected. To move it to another house you cannot teleport the room; you pack everything into labelled boxes (a format the movers understand), ship the boxes, then unpack at the other end to rebuild the arrangement. Serialization is packing your in-memory objects into a flat stream of text; deserialization is unpacking that text back into live objects.

Why text? Because a Python dict in memory is a tangle of pointers that only this Python process understands. A text file, by contrast, is universal — a spreadsheet, a JavaScript app, a database import tool, or a colleague's editor can all read it. JSON and CSV are two agreed-upon text formats:

  • JSON (JavaScript Object Notation) stores nested, typed data: objects inside lists inside objects. It maps almost one-to-one onto Python dicts and lists, which is why it is the default language of web APIs and config files.
  • CSV (Comma-Separated Values) stores a single flat table: rows and columns, like one sheet of a spreadsheet. It is compact and every spreadsheet program opens it, but it has no nesting and no types.
Key point: both formats are just text. A JSON file and a CSV file are things you can open in Notepad and read with your eyes. The modules simply automate the boring, error-prone conversion in both directions.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers