Skip to content

jsonl

Zero-dependency Python library for reading, writing, and compressing JSON Lines files.

PyPI version Python versions CI Coverage License


import jsonl

jsonl.dump([{"name": "Alice"}, {"name": "Bob"}], "file.jsonl.gz")

for item in jsonl.load("file.jsonl.gz"):
    print(item)

If you know json.dump and json.load, you already know jsonl.


Features

  • Familiar API — same dump/load interface as Python's json module.
  • Streaming by default — iterators in, iterators out. Constant memory.
  • Automatic compression.gz, .bz2, .xz, .zst (Python ≥ 3.14). Detected by extension or magic bytes.
  • Archive support — read/write .zip, .tar.gz, .tar.bz2, .tar.xz natively.
  • URL loading — pass a URL to load() or load_archive() directly.
  • Pluggable serialization — swap in orjson, ujson, or any encoder/decoder via cls.
  • Error tolerance — skip malformed lines instead of crashing.
  • Command-line interface — a json-style jsonl command for shell pipelines.
  • Zero dependencies — pure standard library; single .py file you can vendor.

Fully compliant with the JSON Lines and NDJSON specifications.


Quick Start

Install

pip install py-jsonl

Note

Requires Python 3.8+. No external dependencies.

Write and read

import jsonl

# Write to plain or compressed files
jsonl.dump([{"name": "Alice"}, {"name": "Bob"}], "users.jsonl")
jsonl.dump([{"key": "value"}], "data.jsonl.gz")

# Read (lazy iterator — constant memory)
for user in jsonl.load("users.jsonl"):
    print(user)

# Read from a URL
for item in jsonl.load("https://example.com/data.jsonl"):
    print(item)

Archives

import jsonl

data = [
    ("users.jsonl", [{"name": "Alice"}, {"name": "Bob"}]),
    ("orders.jsonl", [{"id": 1, "total": 99.90}]),
]
jsonl.dump_archive("data.tar.gz", data)

for filename, items in jsonl.load_archive("data.tar.gz"):
    for item in items:
        print(filename, item)

Custom serialization

import orjson
import jsonl

data = [{"name": "Alice", "age": 30}]

jsonl.dump(data, "fast.jsonl", text_mode=False, cls=orjson.dumps)

for item in jsonl.load("fast.jsonl", cls=orjson.loads):
    print(item)

Command line

Installing the package also provides a jsonl command for shell pipelines:

# Recompress and reformat, streaming from stdin to stdout
cat input.jsonl.gz | jsonl --sort-keys > output.jsonl

# Select members from an archive
jsonl --member '2026/*.jsonl' dataset.tar.gz > output.jsonl

See the Command-line interface page for all options and exit codes.


API Overview

Reading

Function Description
jsonl.load File, URL, or file-like → lazy iterator
jsonl.loads JSON Lines string → lazy iterator
jsonl.load_archive Unpack JSONL files from ZIP/TAR archive
jsonl.loader Low-level line-stream deserializer

Writing

Function Description
jsonl.dump Write to file (any format)
jsonl.dumps Serialize to string
jsonl.dump_fork Write to multiple files at once
jsonl.dump_archive Pack into ZIP/TAR archive
jsonl.dumper Low-level generator → formatted lines

Custom Serialization

All functions accept cls and **kwargs for custom encoding/decoding.


Supported Formats

Type Extensions
Plain .jsonl
Compressed .jsonl.gz · .jsonl.bz2 · .jsonl.xz · .jsonl.zst¹
ZIP .zip
TAR .tar · .tar.gz · .tar.bz2 · .tar.xz · .tar.zst¹

¹ Requires Python ≥ 3.14

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.