Download src/buddy_fusion/json_utils.py from build-small-hackathon/hatchimera: direct link, hf CLI and curl.
- Browser
- Download file 716 Bytes
-
https://huggingface.co/spaces/build-small-hackathon/hatchimera/resolve/main/src/buddy_fusion/json_utils.py
- Command line
-
hf download hf://spaces/build-small-hackathon/hatchimera/src/buddy_fusion/json_utils.py
-
curl -L -o json_utils.py https://huggingface.co/spaces/build-small-hackathon/hatchimera/resolve/main/src/buddy_fusion/json_utils.py
716 Bytes
| 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 | |