Python 3.14 ·
✓ verified by execution on 2026-07-28
Working with JSON and CSV
In this lesson, we will explore how to handle JSON and CSV data in Python. Let’s start with some core facts verified against the official Python 3.14 documentation.
Fact 1
Claim: The json.dumps() function serializes a Python object to a JSON formatted string.
json.dumps() turns a Python object into a JSON string — that trailing s is the whole difference from dump(), which writes straight to a file.
Example: Serializing a Python dictionary to a JSON string
Claim: The json.loads() function deserializes a JSON string into a Python object.
json.loads() reads the other direction, parsing a JSON string back into Python dicts and lists. Same rule: loads takes a string, load takes an open file.
Example: Deserializing a JSON string to a Python dictionary
Claim: The csv.reader returns an object that iterates over lines in the given CSV file.
csv.reader() hands you an iterator over the rows of a file, each row a list of strings. It handles the awkward parts of CSV — quoted fields, embedded commas — that break naive line.split(',').
Claim: The csv.writer returns an object responsible for converting data into delimited strings.
csv.writer() does the reverse, quoting and escaping fields as needed. Open the file with newline='', or you will get a blank line between every row on Windows.
Example: Writing lists to delimited string using csv.writer
Claim: The csv.DictReader maps the information in each row to a dictionary.
csv.DictReader is usually the one you want: it reads the header row and gives each subsequent row back as a dict, so you write row['email'] instead of row[3] and stop caring about column order.
Example: Reading CSV rows as dictionaries using csv.DictReader
Parsing CSV files where fields contain commas surrounded by quotes.
Serializing Python objects that have circular references or unsupported types (like datetime) to JSON.
Conceptual Overview
Click to view JSON parsing visualizer
python · visualize
import json# From string to dict (loads)json_str = '{"a": 1}'parsed_dict = json.loads(json_str)print('Parsed:', parsed_dict)# From dict to string (dumps)new_str = json.dumps(parsed_dict)print('Stringified:', new_str)
Check yourself
Which of the following is true regarding: Python dictionaries are exactly the same as JSON objects?
Reveal answer
While similar, JSON requires double quotes for strings and uses true/false/null instead of True/False/None. — While similar, JSON requires double quotes for strings and uses true/false/null instead of True/False/None.
Which of the following is true regarding: jsonload() and json.loads() do the same thing.?
Reveal answer
json.load() reads from a file-like object, whereas json.loads() parses a string. — json.load() reads from a file-like object, whereas json.loads() parses a string.
Which of the following is true regarding: CSV files always use commas as separators?
Reveal answer
CSV stands for Comma-Separated Values, but many 'CSV' files use tabs or semicolons, requiring the 'delimiter' argument in csv.reader. — CSV stands for Comma-Separated Values, but many 'CSV' files use tabs or semicolons, requiring the 'delimiter' argument in csv.reader.
Which of the following is true regarding: You can write a Python set directly to JSON?
Reveal answer
Sets are not JSON serializable by default; they must be converted to lists first. — Sets are not JSON serializable by default; they must be converted to lists first.
Challenges
Challenge 1 +20 XP
Given a JSON string representing a user profile, parse it into a dictionary and print the user's email address.