jsonlines Python Library

4.0.0 · active · verified Mon Apr 06

jsonlines is an active Python library (version 4.0.0) that provides helpers for working with the JSON Lines (also known as NDJSON) text file format. It simplifies reading and writing streams of newline-delimited JSON objects, offering features like transparent handling of string and byte streams, support for optional faster JSON parsers (like `orjson` and `ujson`), built-in data validation, and robust error handling. Its design prevents common pitfalls and ensures standard-compliant line breaking.

Warnings

Install

Imports

Quickstart

The quickstart demonstrates writing and reading JSON Lines data using `jsonlines.open()` for file paths and the `jsonlines.Writer`/`jsonlines.Reader` classes for file-like objects (like `io.StringIO`). It covers basic object serialization, deserialization, and the use of context managers for proper resource handling.

import jsonlines
import io
import os

# Create a dummy file for demonstration
output_file = "example_data.jsonl"
data_to_write = [
    {"id": 1, "name": "Alice", "email": "alice@example.com"},
    {"id": 2, "name": "Bob", "email": "bob@example.com", "status": "active"},
    {"id": 3, "name": "Charlie", "data": {"city": "New York", "zip": "10001"}}
]

# Writing JSON Lines data using the convenience 'open' function
with jsonlines.open(output_file, mode='w') as writer:
    writer.write_all(data_to_write)
print(f"Wrote {len(data_to_write)} records to {output_file}")

# Reading JSON Lines data
print("\nReading records:")
read_records = []
with jsonlines.open(output_file) as reader:
    for obj in reader:
        read_records.append(obj)
        print(obj)

print(f"Total records read: {len(read_records)}")
assert read_records == data_to_write

# Example of writing to an in-memory buffer using Writer class
buffer = io.StringIO()
with jsonlines.Writer(buffer) as writer:
    writer.write({"log_event": "started", "timestamp": "2023-01-01T12:00:00Z"})
    writer.write({"log_event": "processed", "item_id": 123})

buffer.seek(0) # Reset buffer position to read
print("\nReading from in-memory buffer:")
with jsonlines.Reader(buffer) as reader:
    for log_entry in reader:
        print(log_entry)

# Clean up the dummy file
os.remove(output_file)
print(f"\nCleaned up {output_file}")

view raw JSON →