Skip to content

JSON Processing

Robust parsing, serialization, and handling of malformed JSON.


parse_json

Python
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

Python
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 silent is True.

show_json_tree

Python
show_json_tree(d)

Builds a tree-like structure as a multiline string with only dictionary keys.

Parameters

d : dict The dictionary to convert into a tree-like structure.

Returns

str The formatted tree as a multiline string.

robust_serializer

Python
robust_serializer(obj: Any) -> Any

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

Python
single_quote_decoder(object_hook=None, *args, **kwargs)

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.

Text Only
                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

Python
sanitize_unescaped_quotes_and_load_json_str(s: str, strict=False) -> dict

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.