INNER CODE UNIT · Python
extract_json_path
Far-Se/tabame · plugins/api-client/main.py:233
def extract_json_path(body_text, path):
try:
data = json.loads(body_text)
except Exception:
return None
cur = data
if not path:
return cur if not isinstance(cur, (dict, list)) else None
for tok in re.findall(r"[^.\[\]]+|\[\d+\]", path):
if tok.startswith("[") and tok.endswith("]"):
idx = int(tok[1:-1])
if isinstance(cur, list) and 0 <= idx < len(cur):
cur = cur[idx]
else:
return None
else:
if isinstance(cur, dict) and tok in cur:
cur = cur[tok]