jsonl¶
A lightweight, dependency-free Python library for reading, writing, and streaming JSON Lines data.
jsonl provides a simple, Pythonic API for working with JSON Lines data.
It follows the conventions of Python's standard json module — if you know json.dump and json.load,
you already know how to use jsonl.
Fully compliant with the JSON Lines and NDJSON specifications.
Why jsonl?¶
The JSON Lines format is ideal for processing large volumes of structured data line by line,
without loading everything into memory. jsonl lets you work with this format effortlessly, offering a familiar API
inspired by Python's standard json module — with zero external dependencies.
Features¶
| Feature | Description |
|---|---|
| 🌎 Familiar API | Interface similar to the standard json module (dump, load, dumps) |
| ⚡ Streaming by default | Read and write incrementally via iterators, keeping memory usage low |
| 🗜️ Built-in compression | Transparent support for gzip, bzip2, and xz |
| 📦 Archive support | Read and write ZIP and TAR archives (.tar.gz, .tar.bz2, .tar.xz) |
| 📥 Load from URLs | Pass a URL directly to load() or load_archive() |
| 🚀 Pluggable serialization | Swap in orjson, or any JSON library |
| 🔧 Error tolerance | Optionally skip malformed lines instead of crashing |
| 🐍 Zero dependencies | Uses only the Python standard library — nothing else |
Quick Start¶
Install¶
Note
Requires Python 3.8 or higher. No external dependencies needed.
Write data¶
import jsonl
data = [
{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]},
{"name": "May", "wins": []},
]
jsonl.dump(data, "players.jsonl")
Read data¶
Read from a URL¶
Compressed files¶
The compression format is detected automatically — by file extension when writing, and by magic numbers when reading if the file extension is not recognized:
import jsonl
data = [{"key": "value"}]
jsonl.dump(data, "file.jsonl.gz") # gzip
jsonl.dump(data, "file.jsonl.bz2") # bzip2
jsonl.dump(data, "file.jsonl.xz") # xz
for item in jsonl.load("file.jsonl.gz"):
print(item)
Archives (ZIP / TAR)¶
import jsonl
# Write multiple files into an archive
data = [
("users.jsonl", [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]),
("orders.jsonl", [{"id": 1, "total": 99.90}, {"id": 2, "total": 45.00}]),
]
jsonl.dump_archive("data.tar.gz", data)
# Read them back
for filename, items in jsonl.load_archive("data.tar.gz"):
print(f"--- {filename} ---")
for item in items:
print(item)
API Overview¶
Reading¶
| Function | Description |
|---|---|
jsonl.load |
Read a file, URL, or file-like object as an iterator of objects |
jsonl.load_archive |
Read JSON Lines files from a ZIP or TAR archive |
Writing¶
| Function | Description |
|---|---|
jsonl.dump |
Write an iterable of objects to a JSON Lines file |
jsonl.dumps |
Serialize an iterable into a JSON Lines string |
jsonl.dump_fork |
Write to multiple JSON Lines files simultaneously |
jsonl.dump_archive |
Pack multiple JSON Lines files into a ZIP or TAR archive |
Custom Serialization
All write functions accept json_dumps and **json_dumps_kwargs for custom serialization.
All read functions accept json_loads and **json_loads_kwargs for custom deserialization.
Supported Formats¶
| Type | Extensions |
|---|---|
| Plain | .jsonl |
| Compressed | .jsonl.gz, .jsonl.bz2, .jsonl.xz |
| ZIP archive | .zip |
| TAR archive | .tar, .tar.gz, .tar.bz2, .tar.xz |
Info
When reading, if the file extension is not recognized, jsonl falls back to magic-number detection to identify the compression format automatically.
License¶
MIT — see LICENSE for details.