JSON Streams

0.6.0 · active · verified Fri Apr 17

jsonstreams is a Python library designed for efficiently writing large JSON files with low memory usage. It provides a context manager-based API for incrementally constructing JSON arrays and objects. The current version is 0.6.0, and while its release cadence is infrequent, the project appears to be actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `jsonstreams.Stream` to write a JSON array of objects to an in-memory buffer. The `with` statements ensure proper handling and closure of the JSON structure, which is crucial for correct output.

import io
from jsonstreams import Stream

# Stream to an in-memory buffer (StringIO) for demonstration
output_buffer = io.StringIO()

with Stream(output_buffer) as s:
    s.start_array()
    with s.array_item() as item:
        item.write({"id": 1, "name": "Apple", "price": 1.0})
    with s.array_item() as item:
        item.write({"id": 2, "name": "Banana", "price": 0.5})
    s.end_array()

# Print the generated JSON string
print(output_buffer.getvalue())
# Expected output: [{"id": 1, "name": "Apple", "price": 1.0}, {"id": 2, "name": "Banana", "price": 0.5}]

view raw JSON →