Spaces:
Running on Zero
Running on Zero
| from __future__ import annotations | |
| import json | |
| from typing import Any | |
| def extract_json_object(text: str) -> str | None: | |
| decoder = json.JSONDecoder() | |
| for index, char in enumerate(text): | |
| if char != "{": | |
| continue | |
| try: | |
| _, end = decoder.raw_decode(text[index:]) | |
| except json.JSONDecodeError: | |
| continue | |
| return text[index : index + end] | |
| return None | |
| def parse_json_object(text: str) -> dict[str, Any] | None: | |
| raw = extract_json_object(text.strip()) | |
| if raw is None: | |
| return None | |
| try: | |
| value = json.loads(raw) | |
| except json.JSONDecodeError: | |
| return None | |
| return value if isinstance(value, dict) else None | |