MCPcopy Create free account
hub / github.com/CJackHwang/AIstudioProxyAPI / extract_json_from_text

Function extract_json_from_text

api_utils/utils_ext/string_utils.py:7–21  ·  view source on GitHub ↗

Attempt to extract the first JSON object string from plain text.

(text: str)

Source from the content-addressed store, hash-verified

5
6
7def extract_json_from_text(text: str) -> Optional[str]:
8 """Attempt to extract the first JSON object string from plain text."""
9 if not text:
10 return None
11 # Simple heuristic: find the first '{' and the last matching '}'
12 try:
13 start = text.find("{")
14 end = text.rfind("}")
15 if start != -1 and end != -1 and end > start:
16 candidate = text[start : end + 1].strip()
17 json.loads(candidate)
18 return candidate
19 except Exception:
20 return None
21 return None
22
23
24def get_latest_user_text(messages: List[Message]) -> str:

Callers 1

maybe_execute_toolsFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected