Partial JSON Parser
The `partialjson` library provides a robust solution for parsing incomplete or partial JSON data in Python. It handles scenarios where JSON strings might be truncated or malformed at the end, returning the largest valid JSON object or array it can parse. The current version is 1.1.0, and releases appear to be infrequent, focusing on stability and bug fixes.
Common errors
-
json.decoder.JSONDecodeError: Expecting value: line X column Y (char Z)
cause This error indicates that `partialjson` encountered an unrecoverable JSON syntax error or non-JSON input that it couldn't even partially parse. It often means the input is more than just incomplete; it's fundamentally malformed.fixEnsure the input string is intended to be JSON and check for severe malformations beyond simple incompleteness (e.g., incorrect syntax within an already established JSON object/array). `partialjson` fixes *trailing* incompleteness, not internal syntax errors. -
ModuleNotFoundError: No module named 'partialjson'
cause The `partialjson` library is not installed in your current Python environment.fixInstall the library using pip: `pip install partialjson` -
TypeError: expected string, bytes or os.PathLike object, not X
cause `partialjson.loads()` expects a string, bytes, or path-like object as its input, but received an incompatible type (e.g., an integer, list, or dictionary).fixEnsure the argument passed to `partialjson.loads()` is a string (or bytes/path-like object) representing the JSON data.
Warnings
- gotcha The `partialjson` library is specifically designed to handle *incomplete* or *truncated* JSON strings, typically at the end of the input. It does not attempt to correct arbitrary syntax errors or malformed structures that occur internally within an otherwise complete JSON segment (e.g., missing commas, incorrect data types within values).
- gotcha When a value within a key-value pair (e.g., a string or number) is itself incomplete and cannot be fully parsed, the entire key-value pair might be omitted from the resulting parsed object to maintain JSON validity. For example, `{'key': "partial_stri` might result in `{}`, not `{'key': None}` or similar.
Install
-
pip install partialjson
Imports
- loads
from partialjson import loads
- load
from partialjson import load
- dumps
from partialjson import dumps
Quickstart
from partialjson import loads
# Example 1: Incomplete dictionary
incomplete_json_dict = '{"name": "Alice", "age": 30, "isStudent": tru'
data_dict = loads(incomplete_json_dict)
print(f"Parsed dict: {data_dict}")
# Expected: {'name': 'Alice', 'age': 30}
# Example 2: Incomplete list
incomplete_json_list = '[1, 2, {"item": "value"'
data_list = loads(incomplete_json_list)
print(f"Parsed list: {data_list}")
# Expected: [1, 2]
# Example 3: Deeply incomplete
deep_incomplete = '{"user": {"id": 123, "name": "Bob"}, "products": [{"id": 1'
parsed_deep = loads(deep_incomplete)
print(f"Parsed deep: {parsed_deep}")
# Expected: {'user': {'id': 123, 'name': 'Bob'}, 'products': []}