IntermediateLesson 16 of 305 min read

JSON and CSV: saving and sharing data

Read and write the two most common data formats with the json and csv modules, and build a load/save pattern.

In this lesson you will

  • Convert between Python data and JSON
  • Save and load JSON files
  • Read and write CSV with csv and DictReader
  • Choose the right format

About 21 minutes

Speaking a language every program understands

Your program saves a list of customers. Next week a different program, perhaps written in JavaScript, needs to read it. A plain text file of loose lines will not do. You need an agreed format. Two formats dominate the world of data: JSON, for structured data of any shape, and CSV, for tables that open in Excel. Python reads and writes both with its standard library.

JSON: data as text

JSON looks almost exactly like Python dictionaries and lists, written as text. Websites, apps and APIs use it everywhere.

import json

student = {"name": "Sara", "age": 21, "marks": [72, 91, 85], "active": True, "nickname": None}

text = json.dumps(student)          # Python -> JSON text
print(text)
print(type(text))

back = json.loads(text)             # JSON text -> Python
print(back["marks"][1], type(back))
{"name": "Sara", "age": 21, "marks": [72, 91, 85], "active": true, "nickname": null}
<class 'str'>
91 <class 'dict'>
Python JSON
dict object {}
list, tuple array []
str string
int, float number
True / False true / false
None null

Pretty printing

import json
data = {"name": "Sara", "skills": ["Python", "HTML"], "address": {"city": "Lahore", "zip": "54000"}}
print(json.dumps(data, indent=2))
print(json.dumps(data, indent=2, sort_keys=True)[:40])
{
  "name": "Sara",
  "skills": [
    "Python",
    "HTML"
  ],
  "address": {
    "city": "Lahore",
    "zip": "54000"
  }
}
{
  "address": {
    "city": "Lahore",
 

JSON files: dump and load

import json

settings = {"theme": "dark", "font_size": 14, "recent": ["a.txt", "b.txt"]}

with open("settings.json", "w", encoding="utf-8") as f:
    json.dump(settings, f, indent=2)           # no s: writes to a file

with open("settings.json", encoding="utf-8") as f:
    loaded = json.load(f)                       # no s: reads from a file

print(loaded == settings)
print(loaded["recent"])
True
['a.txt', 'b.txt']
Good to know

Easy to remember: the s stands for string. dumps/loads work with strings; dump/load work with files.

A save-and-load pattern

import json
from pathlib import Path

FILE = Path("todos.json")

def load():
    if FILE.exists():
        return json.loads(FILE.read_text(encoding="utf-8"))
    return []

def save(todos):
    FILE.write_text(json.dumps(todos, indent=2), encoding="utf-8")

todos = load()
print("Loaded", len(todos))
todos.append({"task": "Learn JSON", "done": False})
save(todos)

todos = load()                 # pretend the program restarted
print("After restart:", todos)
Loaded 0
After restart: [{'task': 'Learn JSON', 'done': False}]

This pair of tiny functions, load() and save(), turns any program into one that remembers. You will use exactly this in the project lesson.

Bad JSON

import json

try:
    json.loads("{'name': 'Sara'}")      # single quotes are not valid JSON
except json.JSONDecodeError as e:
    print("Invalid JSON:", e.msg)

print(json.loads('{"name": "Sara"}'))
Invalid JSON: Expecting property name enclosed in double quotes
{'name': 'Sara'}
Common mistake

Trying to save something JSON does not know, such as a datetime or a set. You get TypeError: Object of type set is not JSON serializable. Convert first: list(my_set) or date.isoformat().

CSV: tables that open in Excel

import csv

rows = [
    ["name", "city", "score"],
    ["Sara", "Lahore", 90],
    ["Ali", "Karachi", 72],
    ["Mina", "Multan", 85],
]

with open("students.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerows(rows)

print(open("students.csv").read())
name,city,score
Sara,Lahore,90
Ali,Karachi,72
Mina,Multan,85

Always pass newline="" when opening a CSV file for writing. Otherwise Windows may add blank lines between rows.

import csv
open("students.csv", "w").write("name,city,score\nSara,Lahore,90\nAli,Karachi,72\nMina,Multan,85\n")

with open("students.csv", newline="", encoding="utf-8") as f:
    reader = csv.reader(f)
    header = next(reader)
    for row in reader:
        print(row)
print(header)
['Sara', 'Lahore', '90']
['Ali', 'Karachi', '72']
['Mina', 'Multan', '85']
['name', 'city', 'score']

DictReader and DictWriter: use column names

import csv
open("students.csv", "w").write("name,city,score\nSara,Lahore,90\nAli,Karachi,72\nMina,Multan,85\n")

with open("students.csv", newline="", encoding="utf-8") as f:
    students = list(csv.DictReader(f))

print(students[0])
total = sum(int(s["score"]) for s in students)
print("Average:", round(total / len(students), 1))

top = max(students, key=lambda s: int(s["score"]))
print("Top:", top["name"])

# add a column and save
for s in students:
    s["grade"] = "A" if int(s["score"]) >= 85 else "B"

with open("graded.csv", "w", newline="", encoding="utf-8") as f:
    w = csv.DictWriter(f, fieldnames=["name", "city", "score", "grade"])
    w.writeheader()
    w.writerows(students)
print(open("graded.csv").read())
{'name': 'Sara', 'city': 'Lahore', 'score': '90'}
Average: 82.3
Top: Sara
name,city,score,grade
Sara,Lahore,90,A
Ali,Karachi,72,B
Mina,Multan,85,A

DictReader uses the first row as names, so you write row["score"] instead of remembering that score is column 3. Notice that CSV values always arrive as text: convert with int() or float().

Converting between formats

import csv, json
open("students.csv", "w").write("name,city,score\nSara,Lahore,90\nAli,Karachi,72\n")

with open("students.csv", newline="") as f:
    rows = list(csv.DictReader(f))
for r in rows:
    r["score"] = int(r["score"])

print(json.dumps(rows, indent=2))
[
  {
    "name": "Sara",
    "city": "Lahore",
    "score": 90
  },
  {
    "name": "Ali",
    "city": "Karachi",
    "score": 72
  }
]

Which format when?

Use When
JSON Nested or varied data, settings, web APIs
CSV Flat tables, spreadsheets, exports for Excel
Plain text Logs and simple notes
Practice

Create a dictionary describing a book (title, author, year, tags list), save it to book.json, load it back and print the author and the first tag.

Show one possible answer
import json

book = {"title": "Learn Python", "author": "Sara", "year": 2025, "tags": ["python", "beginner"]}
with open("book.json", "w") as f:
    json.dump(book, f, indent=2)

with open("book.json") as f:
    data = json.load(f)
print(data["author"], data["tags"][0])
Sara python
Practice

Given a CSV of products with a price column, compute the total price using DictReader.

Hint: Convert price with float().

Show one possible answer
import csv

open("products.csv", "w").write("item,price\nPen,45\nBook,320\nBag,1250\n")

with open("products.csv", newline="") as f:
    total = sum(float(r["price"]) for r in csv.DictReader(f))
print("Total:", total)
Total: 1615.0
Use pandas for big tables

For serious data analysis, the pandas package reads CSV and Excel in one line and gives you powerful table tools. The standard csv module is perfect for small, simple jobs.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does json.dumps() return?

  1. A file
  2. A dictionary
  3. A list
  4. A JSON string

dumps = dump to string.

Which function reads JSON from an open file?

  1. json.load(f)
  2. json.loads(f)
  3. json.read(f)
  4. json.open(f)

No s: it reads from a file.

What does Python’s None become in JSON?

  1. none
  2. 0
  3. null
  4. false

None maps to null.

What type are CSV values after reading?

  1. Integers
  2. Floats
  3. Booleans
  4. Text (strings)

Convert with int() or float().

Why use csv.DictReader?

  1. It is faster
  2. You access columns by name
  3. It writes files
  4. It sorts rows

Each row becomes a dictionary.

Summary

  • json.dumps/loads for strings; dump/load for files.
  • JSON maps to dicts, lists, strings, numbers, None.
  • CSV values are text; convert them.
  • DictReader and DictWriter use column names.

Next lesson

Next: dates and times, one of the trickiest parts of any program.

Questions

Can JSON store dates?

Not directly. Store them as text, for example date.isoformat(), and convert back when you load.

Why do I get blank lines in my CSV on Windows?

You forgot newline="" in open().

JSON or a database?

JSON files are great for small data. Use a database like SQLite when data grows or many users change it at once.

Progress is saved in this browser.