Data Formats (CSV, Parquet, JSON Lines)

CSV is comma-separated text with no types: values with commas must be quoted, NULL and empty text get mixed up, and numbers with a dot between thousands…

Write the prices in a CSV file for people to read, $18.00, and DuckDB refuses to add up Bookshop's revenue. A file from a country that writes a dot between thousands is worse: 1.250 is read as 1.25, without a complaint. The problem is the data format: the way a table's rows are written to a file so another program can read them back with the same contents and types. You'll meet three formats most often, and they differ like three ways of keeping a corner shop's sales log. One way: each sale is one line on paper, its columns separated by commas. Another: each sale is a small form where every box is labelled. A third: a ledger split by column: one page holds every city, one page holds every book count, and the cover notes the smallest and largest number on each page. Those are CSV, JSON Lines and Parquet.

One extract, three files

The data is order_lines: Bookshop's 19 order lines already joined with their customer and book (the same as raw_order_lines in the ETL vs ELT lesson), 9 columns. We write it with DuckDB: an analytical database that runs on a laptop as a single program and can read and write files directly with SQL.

COPY order_lines TO 'order_lines.csv' (HEADER);
COPY order_lines TO 'order_lines.jsonl' (FORMAT json);
COPY order_lines TO 'order_lines.parquet' (FORMAT parquet);

Each COPY ... TO writes the whole table to one file. HEADER puts the column names on the CSV's first line. In DuckDB, FORMAT json writes one object per line. Every number in this lesson was measured with DuckDB 1.5.6.

CSV: text separated by commas

CSV (Comma-Separated Values) is just text. Here are its first four lines, plus Maya's order line:

item_id,order_id,created_at,customer,city,book_id,title,qty,price
1,1,2026-09-28 09:15:00,Budi Santoso,London,1,The Rainbow Troops,1,18
2,2,2026-09-28 13:02:00,"Siti Rahma, BSc",Bristol,2,This Earth of Mankind,1,25
3,2,2026-09-28 13:02:00,"Siti Rahma, BSc",Bristol,3,Beauty Is a Wound,1,22
11,8,2026-10-04 20:30:00,Maya Sari,,1,The Rainbow Troops,1,18

Three mistakes that happen often, read back with SELECT SUM(qty * price) AS revenue FROM 'order_lines.csv':

The file was written DuckDB reads price as Revenue result
correctly: 18, names with commas quoted BIGINT 665
price with a decimal comma: 18,00 the comma makes a 10th column, so DuckDB stops taking the first line as the header Binder Error: Referenced column "qty" not found in FROM clause!
price for humans: $18.00 VARCHAR (text) Binder Error: No function matches the given name and argument types '*(BIGINT, VARCHAR)'
names with commas, no quotes DuckDB fails to guess the separator and reads each line as 1 column of text Binder Error: Referenced column "qty" not found in FROM clause!

These three stop with an error. The dangerous mistake is the quiet one: in many countries a dot separates thousands, and a file from there that holds 1.250 (one thousand two hundred and fifty) is read as the number 1.25, with no error. Store numbers as plain numbers, with no thousands separators and no "$".

One more: save CSV as UTF-8. Excel's "CSV UTF-8" option puts a 3-byte marker (a BOM) at the start of the file. DuckDB and pandas drop it, but Python's built-in csv module with encoding="utf-8" reads the first column as 'item_id'. Use encoding="utf-8-sig".

JSON Lines: one object per line

JSON Lines (.jsonl, also called NDJSON) stores one complete JSON object per line:

{"item_id":1,"order_id":1,"created_at":"2026-09-28 09:15:00","customer":"Budi Santoso","city":"London","book_id":1,"title":"The Rainbow Troops","qty":1,"price":18}
{"item_id":11,"order_id":8,"created_at":"2026-10-04 20:30:00","customer":"Maya Sari","city":null,"book_id":1,"title":"The Rainbow Troops","qty":1,"price":18}

Parquet: stored by column

CSV and JSON Lines are row-based: row 1 in full, then row 2 in full, and so on. Parquet is columnar: all the values of one column are written next to each other. What's inside order_lines.parquet, according to DuckDB's parquet_metadata():

Part What it holds in our file
Row group a slice of rows; here 1 row group holds 19 rows (DuckDB makes one per about 122 thousand rows)
Column chunk one per column per row group, compressed with SNAPPY; qty is only 66 bytes
Encoding city and qty use a dictionary: each distinct value is stored once, and rows only store its number
Footer the schema (price = INT64, created_at = TIMESTAMP_MICROS) and per-column statistics: city has 2 NULLs, price min 18 max 190

Look at the price statistics: the 190 typo already shows in the metadata, without reading a single row. This layout gives two things:

The types are stored in the file, so there's no guessing as with CSV. The downsides: Parquet is binary, you can't open it in a text editor or Excel, and a file once written can't take more rows; new data becomes a new file.

Size depends on the row count

Before you look at the table, guess: for Bookshop's 19 rows, which is smaller, CSV or Parquet? And with 1 million rows?

Format 19 rows 1 million rows GROUP BY city at 1 million rows
CSV 1,418 bytes 79.2 MB 0.59 seconds
JSON Lines 3,136 bytes 173.2 MB 0.45 seconds
Parquet (snappy) 2,150 bytes 13.1 MB 0.009 seconds
Parquet (zstd) 2,137 bytes 4.9 MB

The 1 million rows have the same columns and contents: the 7 customers and 4 books repeated, measured with DuckDB on a laptop. At 19 rows, CSV is smallest: Parquet's footer alone is 1,050 bytes. At 1 million rows, Parquet is 6 times smaller than CSV, 16 times with zstd compression, and the query is dozens of times faster. So either one can be smaller, depending on the row count.

💡 Avro and ORC

Avro is row-based like JSON Lines, but binary and compact: its schema (written in JSON) is stored once at the start of the file, then each record is written without column names. Avro handles schema changes well, such as adding a column with a default value, so it's often used for Kafka messages together with a Schema Registry.

ORC (Optimized Row Columnar) is columnar like Parquet, born in the Hadoop and Hive ecosystem. Its layout is similar (slices of rows called stripes, statistics, per-column compression). ORC is still common in Hive, Trino and Spark; outside the Hadoop world, Parquet is used far more.

⚠️ What makes a data file read wrong, or slowly

TL;DR: CSV is comma-separated text with no types: values with commas must be quoted, NULL and empty text get mixed up, and numbers with a dot between thousands (1.250) are read wrong without an error. JSON Lines stores one JSON object per line, so it can be processed line by line (good for logs and streaming) and keeps its types, but it's the biggest. Parquet stores data by column in row groups, with dictionary encoding, compression and a footer holding the schema and min/max statistics, so a query reads only the columns and row groups it needs. At 19 rows CSV is smallest (1,418 bytes); at 1 million rows Parquet is 13.1 MB against CSV's 79.2 MB. Avro is binary row-based with a schema (Kafka); ORC is columnar from the Hadoop world.

⚡ CSV: a comma inside a value

Write:

2,2,2026-09-28 13:02:00,"Siti Rahma, BSc",Bristol,2,This Earth of Mankind,1,25

Meaning: A value containing a comma is wrapped in double quotes. Without them, the comma in Siti's name is read as a column separator. CSV has no types either: the reader guesses.

⚡ A dot between thousands

Guess first: a marketplace's CSV writes one thousand two hundred and fifty as 1.250, the way many countries do. What does DuckDB read?

The number 1.25. The dot is taken as a decimal point, and there's no error at all. Store plain numbers, with no thousands separators and no "$".

Meaning: Number formatting belongs to the display, not the file.

⚡ JSON Lines: one object per line

Write:

{"item_id":11,"order_id":8,"created_at":"2026-10-04 20:30:00","customer":"Maya Sari","city":null,"book_id":1,"title":"The Rainbow Troops","qty":1,"price":18}

Meaning: Each line is complete JSON, so it can be read and processed one at a time without parsing the whole file. Good for logs and streaming, and null stays null. But it's the biggest.

⚡ Parquet: by column

Guess first: at 19 rows, which is smaller, CSV or Parquet?

CSV, 1,418 bytes. Parquet's footer alone is 1,050 bytes. Only at 1 million rows does it flip: Parquet 13.1 MB, CSV 79.2 MB.

Meaning: Parquet stores data by column in row groups, with a footer holding the schema and min/max statistics. A query reads only the columns and row groups it needs.

What you will learn