json-part
json-part is a lightweight Python library designed to parse and
repair incomplete JSON strings. This library is particularly useful
for handling streaming JSON data, such as the responses from
OpenAI's Large Language Models (LLMs). By enabling the processing
of partial JSON responses, json-part enhances the user experience
by providing immediate, incremental results.
Currently, there is no standard for loading such data, and the project has made many assumptions which, in my opinion, were correct. A fully generic, flexible solution was not prepared. The library code is simple and clear -- requests for contributions, or ideas submitted as an Issue, will be met with all efforts to create the best possible solution.
Features
- Parse Incomplete JSON: Effortlessly convert partial JSON strings into usable Python dictionaries.
- Fix Incomplete JSON: Automatically repair and close open JSON structures to prevent parsing errors.
- Stream-Friendly: Ideal for applications that consume streaming JSON data, improving responsiveness and user interaction.
Installation
To get started with json-part, simply install the package using pip:
pip install json-part
Usage
The json-part library is straightforward to use, with functions
to parse and fix incomplete JSON strings. Here's a quick
example to demonstrate its capabilities:
from json_part import parse_incomplete_json, fix_incomplete_json
if __name__ == '__main__':
# Example of an incomplete JSON string
SAMPLE_1 = """{"status": "o""" # {"status": "o
# Parse the incomplete JSON to a Python dictionary
print(parse_incomplete_json(SAMPLE_1)) # Output: {'status': 'o'}
# Fix the incomplete JSON string
print(fix_incomplete_json(SAMPLE_1)) # Output: {"status": "o"}
# Another example with a slightly more complex incomplete JSON string
SAMPLE_2 = """{"status": "o", "mess""" # {"status": "o", "mess
# Parse and fix operations can handle more complex structures
print(parse_incomplete_json(SAMPLE_2)) # Output: {'status': 'o'}
print(fix_incomplete_json(SAMPLE_2)) # Output: {"status": "o"}
Contributing
Contributions to json-part are welcome!
Whether it's reporting a bug, discussing improvements,
or submitting a pull request, all contributions
help make json-part better for everyone.
License
json-part is released under the MIT License.
See the LICENSE file for more details.
Metadata
Release files for json-part 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| json_part-0.1.1.tar.gz | 4.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| json_part-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / json_part-0.1.1.tar.gz
| Download URL | json_part-0.1.1.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4182124e3a94899938544e41bcfbe7a37284b1109aca9b042bddfe4d65a94f99
|
|
BLAKE2b-256 checksum How to use checksums |
2674ffed5f6237da9f8c6a9298ce5abd5ae33c8284bd69b28669e38500d14181
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure
|
Release files / json_part-0.1.1-py3-none-any.whl
| Download URL | json_part-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
919735311d0192e8e9d132293c91e96ef589c8509a2bb43b54c486aaa40c9a80
|
|
BLAKE2b-256 checksum How to use checksums |
239aa38b8b84b45a8416ca096050e64f730feb5362681349fc6dbae13d0b2344
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure
|