Skip to main content

CNRI JSON Streaming

This library provides a streaming API for reading JSON from inputs including file-like objects. This can be useful when needing to read from very large JSON objects. Instead of reading the entire object into memory, objects can be read off of it one by one.

The current implementation uses ijson. Exceptions are raised as ijson.JsonError.

Installation

pip install cnri_json_streaming

Usage

The library provides two main ways to parse JSON:

  1. JsonReader - A streaming reader for incremental JSON parsing
  2. json_parse - A simple function for parsing entire JSON content using JsonReader

Using JsonReader

The JsonReader class provides a high level of control over the parsing process, allowing you to read JSON incrementally:

from urllib.request import urlopen
from cnri_json_streaming import JsonReader

## Create a reader from a urlopen response
with urlopen('https://example.com/large_data.json') as response:
    with JsonReader(response.read()) as json_reader:
        # Read a JSON object incrementally
        json_reader.start_map()
        while json_reader.has_next():
            property_name = json_reader.next_map_key()
            if property_name == 'results':
                # Read an array of objects
                json_reader.start_array()
                while json_reader.has_next():
                    result = json_reader.next_json()
                    print(result['id'])
                json_reader.end_array()
            elif property_name == 'size':
                # Read a number
                count = json_reader.next_json()
                print(f"Total results: {count}")
            else:
                # Skip properties we don't care about
                print(f"Skipping property: {property_name}")
                json_reader.skip_value()
        json_reader.end_map()

Using json_parse

The json_parse function provides a way to parse JSON from various input sources including file-like objects. json_parse should be more memory-efficient than reading an entire stream into a string and then using json.loads. This function is mostly used for testing.

from urllib.request import urlopen
from cnri_json_streaming import json_parse

#  Parse JSON from a string
data = json_parse('{"item": "Widget", "count": 1000}')
print(data['item'])  # "Widget"

# Parse JSON from a fetch response
with urlopen('https://example.com/large_data.json') as response:
    result = json_parse(response.read())
    print(result['size'])
    print(result['results'][0])
    print(result['results'][0]['id'])

Metadata

Release files for cnri-json-streaming 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cnri-json-streaming 1.0.0
File Size Uploaded
cnri_json_streaming-1.0.0.tar.gz 11.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cnri-json-streaming 1.0.0
File Interpreter ABI Platform
cnri_json_streaming-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / cnri_json_streaming-1.0.0.tar.gz

Download URL cnri_json_streaming-1.0.0.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a8a4e8fed3c416e5a62c6c988131ff27c483e2514975576adfaf487ab72e5a8c
BLAKE2b-256 checksum
How to use checksums
66f00c065442789fcbe56ad250234b957dca0fc4ada9685e93572136a3acb358
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / cnri_json_streaming-1.0.0-py3-none-any.whl

Download URL cnri_json_streaming-1.0.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dff105d8140fbd142a42b4f6c52cc075c0a48f9bb86971de84b03e7b10cbd866
BLAKE2b-256 checksum
How to use checksums
42ee265ed7deb644b0d4c32083e48a95ca42d3247a5dbc09a218e786b3909eec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page