Python library to help parsing broken and invalid JSON.
Project description
parse_broken_json
A small library to help parse broken or invalid JSON strings
Overview
parse_broken_json is a Python library designed to parse malformed or incomplete JSON strings that would typically fail with standard JSON parsers. It's particularly useful when dealing with:
- Truncated JSON responses from APIs or LLMs
- Streaming JSON data that arrives incomplete
- JSON with missing closing brackets or braces
- Partial JSON extractions from text
Installation
Install with uv:
uv add parse_broken_json
Install with pip:
pip install parse_broken_json
Usage
The library provides a single function:
parse_broken_json(text: str, allowed_keys: list[str]) -> Any
Parameters:
text: The JSON string to be parsed (can be broken or incomplete)allowed_keys: List of keys allowed in the JSON nodes (helps guide the parser)
Returns: The parsed Python object (dict, list, etc.)
Example
from parse_broken_json import parse_broken_json
# Parse incomplete JSON
broken_json = '{"name": "John", "age": 30, "address": {"city": "New'
allowed_keys = ["name", "age", "address", "city", "street"]
result = parse_broken_json(broken_json, allowed_keys)
print(result) # {'name': 'John', 'age': 30, 'address': {'city': 'New'}}
Implementation
Internally, this library uses the interpreter pattern to parse and recover from malformed JSON structures.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parse_broken_json-0.3.tar.gz.
File metadata
- Download URL: parse_broken_json-0.3.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85d521a386c36d8674e03c84a47fdf9ff316ff5d580f573e90ec78d6fce74468
|
|
| MD5 |
81f1393a26bd541deb6b5c962ee23f99
|
|
| BLAKE2b-256 |
3a8790af57aed8a467fd577a8e760b96c65faf1233b8a9ead98efdfb9d1d9b16
|
File details
Details for the file parse_broken_json-0.3-py3-none-any.whl.
File metadata
- Download URL: parse_broken_json-0.3-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02995c7270309d66a5f16a5e08db1acd7ae4ce80b3e0eb18c99b2495296c86d9
|
|
| MD5 |
c9f0d35acf0e6131a9c6503a408f4c81
|
|
| BLAKE2b-256 |
ab2d7d951154d14f2c4da51b9c2701959829fe3ace72166939a8ceeeb18a6db6
|