Partial JSON Parser
Partial JSON Parser is a lightweight and customizable Python library designed to parse incomplete or partially received JSON strings, commonly generated by Large Language Models (LLMs). It enables incremental processing of JSON data as it streams in, preventing errors that traditional `json.loads` would throw on incomplete input. The library is pure Python, has no external dependencies, and supports Python 3.6+.
Warnings
- gotcha The `loads` function returns a Python object representing the parsed (potentially incomplete) JSON. If you need a *syntactically complete JSON string*, use `ensure_json` instead.
- gotcha The `allow_partial` argument (using the `Allow` enum) is crucial for controlling what types of partialness the parser should tolerate. If not specified, it defaults to `Allow.ALL`, which might not always be the desired behavior for strict parsing.
- gotcha This library is designed for *partial* JSON (e.g., a string cut off mid-value or before an object is closed), not for *arbitrarily malformed* JSON with fundamental syntax errors like unquoted keys or missing commas where they should be. It assumes the underlying structure is mostly valid but incomplete.
Install
-
pip install partial-json-parser
Imports
- loads
from partial_json_parser import loads
- Allow
from partial_json_parser import Allow
- ensure_json
from partial_json_parser import ensure_json
Quickstart
from partial_json_parser import loads, Allow, ensure_json
# Example 1: Basic partial JSON parsing
partial_string_1 = '{"name": "Alice", "age": 3'
parsed_data_1 = loads(partial_string_1)
print(f"Parsed data (basic): {parsed_data_1}")
# Example 2: Parsing with specific allowed partialness (partial string and object)
partial_string_2 = '{"user": "John D'
parsed_data_2 = loads(partial_string_2, allow_partial=Allow.STR | Allow.OBJ)
print(f"Parsed data (with Allow): {parsed_data_2}")
# Example 3: Ensuring a complete JSON string
partial_string_3 = '{"city": "New York', "temp": 75'
completed_json_str = ensure_json(partial_string_3)
print(f"Completed JSON string: {completed_json_str}")
# Example 4: Edge case - empty string
empty_string_parsed = loads('')
print(f"Parsed empty string: {empty_string_parsed}")