NDJSON Decoder for Python

0.3.1 · active · verified Fri Apr 10

The `ndjson` library for Python, currently at version `0.3.1`, provides a `JsonDecoder` and `JsonEncoder` for newline-delimited JSON (NDJSON), also known as JSON Lines. It offers a familiar interface similar to Python's built-in `json` module, enabling efficient reading and writing of NDJSON data to and from file-like objects and strings. This lightweight library has no external dependencies and is particularly useful for processing large datasets or streaming applications where each line represents a complete, independent JSON object. Although its last release was in 2020 and its PyPI status is 'Pre-Alpha', it is considered stable and functional for its stated purpose.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to write and read NDJSON data using the `ndjson` library's `dump`/`load` functions for bulk operations and `writer`/`reader` classes for streaming line-by-line processing, similar to Python's `csv` module. This is particularly efficient for large files, avoiding the need to load the entire dataset into memory.

import ndjson
import os

# Example data
data_to_write = [
    {"name": "Alice", "age": 30, "city": "New York"},
    {"name": "Bob", "age": 24, "city": "San Francisco"},
    {"name": "Charlie", "age": 35, "city": "London"}
]

file_path = "example.ndjson"

# --- Writing NDJSON to a file ---
with open(file_path, 'w', encoding='utf-8') as f:
    # Using ndjson.dump for a list of objects
    ndjson.dump(data_to_write, f)
print(f"Data written to {file_path} using ndjson.dump")

# Alternatively, using ndjson.writer for streaming individual rows
file_path_writer = "example_writer.ndjson"
with open(file_path_writer, 'w', encoding='utf-8') as f:
    writer = ndjson.writer(f)
    for record in data_to_write:
        writer.writerow(record)
print(f"Data written to {file_path_writer} using ndjson.writer")

# --- Reading NDJSON from a file ---
read_data_dump = []
with open(file_path, 'r', encoding='utf-8') as f:
    # Using ndjson.load for reading all objects from a file
    read_data_dump = ndjson.load(f)
print(f"\nData read from {file_path} (ndjson.load):\n{read_data_dump}")

# Alternatively, using ndjson.reader for streaming individual rows
read_data_reader = []
with open(file_path_writer, 'r', encoding='utf-8') as f:
    reader = ndjson.reader(f)
    for row in reader:
        read_data_reader.append(row)
print(f"\nData read from {file_path_writer} (ndjson.reader):\n{read_data_reader}")

# Clean up created files
os.remove(file_path)
os.remove(file_path_writer)

view raw JSON →