JSON Processing¶
Robust parsing, serialization, and handling of malformed JSON.
parse_json
¶
parse_json(response: Union[str, dict], max_depth: int = 25, verbose=False, print_tree=False) -> dict
Entry point to parse a JSON response into a Python dictionary. Handles nested JSON structures and enforces a maximum recursion depth.
Parameters¶
response : Union[str, dict] The JSON response to parse. Can be a dictionary or a JSON-encoded string. max_depth : int, optional Maximum allowed recursion depth to prevent infinite loops (default is 25). verbose : bool, optional If True, logs information messages for key parsing. Otherwise, logs debug messages (default is False). print_tree: bool, optional If true, prints the parsed JSON tree (default is False). Returns
dict Parsed JSON response as a Python dictionary.
Raises¶
RecursionError If the maximum recursion depth is exceeded. TypeError If the response cannot be parsed into a dictionary. ValueError If the JSON is invalid. json.JSONDecodeError If the JSON decoding fails.
Notes¶
This function calls recur_parse_json to handle nested structures and uses safe_json_loader
to handle malformed JSON gracefully.
safe_json_loader
¶
safe_json_loader(content: Any, raise_error=False, depth=0, verbose=False) -> Union[dict, str, Any]
Safely parses JSON strings into Python dictionaries or leaves them as-is if they are malformed.
Parameters¶
content : Any The content to parse. Expected to be a JSON string, dictionary, or list. raise_error : bool, optional If True, raises exceptions for JSON parsing errors. Otherwise, leaves malformed strings as-is (default is False). depth : int, optional Current recursion depth, used for logging indentation (default is 0). verbose : bool, optional If False, suppresses warnings for malformed JSON (default is False).
Returns¶
Union[dict, str, Any] Parsed JSON object, string, or the original content if parsing fails.
Raises¶
json.JSONDecodeError
If JSON decoding fails and raise_error is True.
TypeError
If the content is not a string, dictionary, or list.
Notes¶
- Properly handles nested structures by recursively calling itself for strings within dictionaries.
- Logs warnings for malformed JSON strings unless
silentis True.
show_json_tree
¶
robust_serializer
¶
JSON serializer for objects not serializable by default JSON code.
Handles common non-serializable types: - datetime/date → ISO 8601 string - Decimal → float - UUID → string - Path → string - Enum → value or name - set/frozenset → list - bytes/bytearray → hex string - Pydantic models → dict via .model_dump() or .dict() - Dataclasses → dict via asdict() - Custom objects with dict → dict - Fallback → str()
RobustJSONEncoder
¶
JSONEncoder subclass that uses robust_serializer for unsupported objects.
single_quote_decoder
¶
A custom JSON decoder that preprocesses JSON strings to handle single quotes, Markdown block markers, and unescaped double quotes within string values. This is useful when decoding JSON output from LLMs as they often use incorrect syntax.
This class extends the default json.JSONDecoder to allow for the decoding of JSON strings that:
- Use single quotes for keys and values instead of double quotes.
- May include Markdown block markers for JSON code blocks.
- Contain unescaped double quotes within string values.
The return value of `object_hook` will be used instead of the `dict`. This can be used to
provide custom deserializations (e.g., to support JSON-RPC class hinting).
Usage example: >>> import json >>> json_str = "{'name': 'John', 'age': 30, 'city': 'New York'}" >>> decoded_obj = json.loads(json_str, cls=single_quote_decoder) >>> print(decoded_obj)
sanitize_unescaped_quotes_and_load_json_str
¶
Sanitizes a JSON string by escaping unescaped quotes and then loads it into a dictionary.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
The loaded JSON object as a dictionary. |