https://github.com/zaironjacobs/streamjson
Write to or read from JSON file using a stream.
https://github.com/zaironjacobs/streamjson
json python stream streamjson
Last synced: about 1 month ago
JSON representation
Write to or read from JSON file using a stream.
- Host: GitHub
- URL: https://github.com/zaironjacobs/streamjson
- Owner: zaironjacobs
- License: mit
- Created: 2021-12-11T16:27:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-13T01:05:01.000Z (4 months ago)
- Last Synced: 2025-04-30T09:24:36.264Z (about 2 months ago)
- Topics: json, python, stream, streamjson
- Language: Python
- Homepage:
- Size: 27.3 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StreamJSON
[](https://pypi.python.org/pypi/streamjson)
[](https://pypi.python.org/pypi/streamjson)
[](https://pypi.python.org/pypi/streamjson)[](https://github.com/zaironjacobs/streamjson/actions/workflows/test.yml)
Write objects or arrays to a JSON file using a stream. Useful for when you don't want to read large amounts of data in
memory, for example when you need to save large amounts of data from a database to a single JSON file.Read objects from a JSON file using a stream. Does not require loading the whole JSON file in memory.
## Install
```console
pip install streamjson
```## How to write to JSON file
Fetch data from a database or anywhere else and send to a JSON file. The send function takes in a dictionary or a list.
A new file with the given name will be created, the root of the JSON file is an array by default.```Python
from streamjson import StreamJSONWriterpersons = [{'id': '0001', 'first_name': 'John', 'last_name': 'Doe'},
{'id': '0002', 'first_name': 'Jane', 'last_name': 'Doe'}]with StreamJSONWriter('persons.json', indent=2) as writer:
for person in persons:
writer.send(person)
```persons.json:
```JSON
[
{
"id": "0001",
"first_name": "John",
"last_name": "Doe"
},
{
"id": "0002",
"first_name": "Jane",
"last_name": "Doe"
}
]
```## How to read from JSON file
The reader will stream each object from the JSON file.
```Python
from streamjson import StreamJSONReaderwith StreamJSONReader('persons.json') as reader:
for obj in reader.find():
print(obj)
```