Skip to main content

Send objects or arrays to a JSON file using a stream. Read objects from a JSON file using a stream.

Project description

StreamJSON

PyPI - Python Version PyPI PyPI - Status PyPI - License

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

To install:

pip install streamjson

To upgrade:

pip install streamjson --upgrade

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.

from streamjson import StreamJSONWriter

persons = [{'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:

[
  {
    "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.

from streamjson import StreamJSONReader

with StreamJSONReader('persons.json') as reader:
    for obj in reader.find():
        print(obj)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamjson-1.0.2.tar.gz (6.3 kB view details)

Uploaded Source

File details

Details for the file streamjson-1.0.2.tar.gz.

File metadata

  • Download URL: streamjson-1.0.2.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for streamjson-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f0749a68efdeeaf3b0053652bee73c4d17f09ef7b3658e7626b1da175498e40b
MD5 e2df9fcd3ff4eaa8299b8ab79bdfb0e8
BLAKE2b-256 13dff1a19b99526926c6952be8184b1324655ab85e4ee03bf3e2e88f9c5449e3

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page