Row-Based Formats: CSV and JSON
Handle CSV and JSON correctly, including delimiters, quoting, encodings, nested data and JSON Lines, and know when they are the wrong choice.
Beginner · about 4 hours · difficulty 2/5
Learn first: Working with Files and DataFrames in Python
CSV and JSON are how most data arrives: exports from partners, API responses, log files. They are human-readable and universal, and they cause a surprising number of production bugs.
CSV
- There is a loose standard (RFC 4180) but every tool interprets it slightly differently.
- Watch for delimiters inside values (addresses with commas), quoting and escaping, embedded newlines, headers and encodings (UTF-8 vs Windows-1252; Hindi or Tamil names turning into garbage).
- CSV has no types. Everything is text until you parse it, so dates like 03/04/2026 are ambiguous (March or April?).
JSON and JSON Lines
- JSON supports nesting (an order with an array of items) and basic types.
- JSON Lines (one object per line) is splittable and streamable, unlike one giant JSON array.
- Nested data must be flattened or exploded for tables: one order row with three items becomes three item rows.
When they are the wrong choice
For storage and analytics, row text formats are large, slow to scan and lack schemas. Convert to a columnar format like Parquet as early as possible, while keeping the raw files for replay.
Compression
Gzip shrinks text a lot but a single gzipped file cannot be split across workers. Prefer many moderately sized files or splittable codecs.
Pitfalls
- Opening a CSV in Excel and saving it, which mangles dates, leading zeros and long IDs.
- Assuming every row has the same JSON keys. Missing keys and type changes (string vs number) are common.
You are ready to move on when you can load a messy partner CSV with quoted commas, a non-UTF-8 encoding and ambiguous dates without losing or corrupting a single row, and flatten a nested JSON order into item rows.
Key concepts
- CSV quoting and delimiters
- Character encodings
- JSON types and nesting
- JSON Lines
- Flattening and exploding arrays
- Splittable vs non-splittable compression
- XML and fixed-width files: Still common in banks and government
Mini project
Create a deliberately messy CSV (quoted commas, newlines inside fields, Hindi names, mixed date formats) and a nested JSON order file. Write a Python loader that parses both correctly into clean tables and logs any rejected rows.
Check yourself
Why is one 10 GB gzipped CSV harder for a distributed engine to process than one hundred 100 MB files?
Free resources
- RFC 4180: CSV format (IETF)
- JSON Lines (jsonlines.org)
- Introducing JSON (json.org)