Skip to main content

A JSON parser capable of parsing incomplete JSON strings from streams/generators.

Project description

JSON Streamer

JSON Streamer is an opportunistic parser designed for processing incomplete JSON strings in real-time from streams.

This can be very useful for parsing JSON strings from network streams, such as websockets and don't want to wait for the entire JSON string to be received before acting on it.

A good use-case is for parsing OpenAI's GPT response with streaming completion enabled or even with functions enabled. (This is actually a real use-case! see how openai-streaming use this package just for that)

Installation

Install the package using pip:

pip install json-streamer

Features

  • Real-time Parsing: Process incomplete JSON strings as they are streamed.
  • Flexibility: Built on a generator-based architecture for ease of integration.
  • Partial Object Handling: Yields partially parsed JSON objects to be acted upon immediately.

Usage

Basic Usage

from json_streamer import loads


# Define a stream of JSON data
def stream_json():
    json_string = '{"field1": "value1", "field2": 42}'
    chunk_size = 4
    for i in range(0, len(json_string), chunk_size):
        yield json_string[i:i + chunk_size]


# Create a stream generator
mystream = stream_json()

# Use the 'loads()' function to parse the stream
ret = loads(mystream)

# Iterate through the generator to get parsed JSON objects
for s in ret:
    print(s)  # Output will be tuples of (ParseState, JSON object)

The loads() function returns a generator that yields tuples of (ParseState, JSON object).

Using with Generator receivers

Utilize the parser with a generator receiver for additional control:

from json_streamer import loads

# Initialize the parser generator
parser = loads()
next(parser)

# Stream and send chunks to the parser
for chunk in stream_json():
    recv = parser.send(chunk)
    print(recv)  # Output will be tuples of (ParseState, JSON object)

How it Works

The parser internally maintains a stack of opening symbols like brackets, braces, and quotes. Upon each new data chunk, it attempts to match these with closing symbols to parse available JSON objects.

License

This project is licensed under the terms of the MIT license.

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

json-streamer-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

json_streamer-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file json-streamer-0.1.0.tar.gz.

File metadata

  • Download URL: json-streamer-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for json-streamer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 928716960d2252e7fb80a48aafefdde3b4ba8a38abeca7feb7bf332211158c91
MD5 b4655743f8df4c0cce5ac0e991f7f268
BLAKE2b-256 9119a99d10a24cc1abcdffd1b87f2a1cd4d4a1faeb31752ba071765d60cd070f

See more details on using hashes here.

File details

Details for the file json_streamer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for json_streamer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db45bef31cd8420fddd61b4c15a9d789c60946b83766f30ce7a30a03ab7c5319
MD5 2a453ff0a1a8433ccffa01ce90e5d763
BLAKE2b-256 d4e1ccf4b6a69d1f34ff79c4b9ea3b558bb8c958709da420217e423d724c9dc2

See more details on using hashes here.

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