{"id":9850,"library":"jsonstreams","title":"JSON Streams","description":"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.","status":"active","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/dcbaker/jsonstreams","tags":["JSON","streaming","serialization","writer","low-memory"],"install":[{"cmd":"pip install jsonstreams","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary class 'Stream' is typically imported directly from the top-level package.","wrong":"import jsonstreams","symbol":"Stream","correct":"from jsonstreams import Stream"},{"note":"Use this import for pretty-printed JSON output.","symbol":"pretty.Stream","correct":"from jsonstreams.pretty import Stream"}],"quickstart":{"code":"import io\nfrom jsonstreams import Stream\n\n# Stream to an in-memory buffer (StringIO) for demonstration\noutput_buffer = io.StringIO()\n\nwith Stream(output_buffer) as s:\n    s.start_array()\n    with s.array_item() as item:\n        item.write({\"id\": 1, \"name\": \"Apple\", \"price\": 1.0})\n    with s.array_item() as item:\n        item.write({\"id\": 2, \"name\": \"Banana\", \"price\": 0.5})\n    s.end_array()\n\n# Print the generated JSON string\nprint(output_buffer.getvalue())\n# Expected output: [{\"id\": 1, \"name\": \"Apple\", \"price\": 1.0}, {\"id\": 2, \"name\": \"Banana\", \"price\": 0.5}]","lang":"python","description":"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."},"warnings":[{"fix":"To read or parse JSON, use Python's built-in `json` module (e.g., `json.load()` or `json.loads()`) or a dedicated JSON parsing library.","message":"`jsonstreams` is a JSON *writer* only. It does not provide functionality for parsing or reading existing JSON data.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always wrap your stream and item operations within `with` statements to ensure the JSON structure is correctly opened and closed, guaranteeing valid JSON and proper resource management.","message":"Proper use of context managers (`with` statements) for `Stream`, `array_item()`, and `object_item()` is critical. Omitting them can lead to malformed JSON output or unclosed streams.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you are running Python 2.7 or Python 3.6 and newer to use `jsonstreams`. Upgrade your Python environment if necessary.","message":"The library explicitly excludes Python versions 3.0 through 3.5. Attempting to install or use it on these versions will fail due to `Requires-Python` metadata.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Use Python's standard `json` module (e.g., `json.load(file_obj)`) or another JSON parsing library to read JSON data. `jsonstreams` cannot be used for this purpose.","cause":"`jsonstreams` is designed exclusively for writing JSON. You are attempting to read data using a `Stream` object.","error":"AttributeError: 'Stream' object has no attribute 'read'"},{"fix":"When within an `array_item` or `object_item` context, ensure `write()` calls are made on the context manager object itself. Example: `with s.array_item() as item: item.write({'key': 'value'})`.","cause":"You called `stream_object.write(...)` directly when inside an `array_item()` or `object_item()` context, which already provides its own `write()` method.","error":"jsonstreams.core.JsonStreamException: Cannot write directly to stream while inside an array or object."},{"fix":"Thoroughly review your `jsonstreams` code. Verify that all `start_` calls have corresponding `end_` calls and that all context managers (`Stream`, `array_item`, `object_item`) are used correctly with `with` statements to ensure proper JSON nesting and closure.","cause":"The JSON file generated by `jsonstreams` is malformed, often due to incorrect ordering of `start_array`/`end_array`, `start_object`/`end_object`, or not using `with` statements for contexts.","error":"json.decoder.JSONDecodeError: Expecting value: line X column Y (char Z)"}]}