MCPcopy Index your code
hub / github.com/dataease/SQLBot / extract_nested_json

Function extract_nested_json

backend/common/utils/utils.py:56–80  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

54
55
56def extract_nested_json(text):
57 stack = []
58 start_index = -1
59 results = []
60
61 for i, char in enumerate(text):
62 if char in '{[':
63 if not stack: # 记录起始位置
64 start_index = i
65 stack.append(char)
66 elif char in '}]':
67 if stack and ((char == '}' and stack[-1] == '{') or (char == ']' and stack[-1] == '[')):
68 stack.pop()
69 if not stack: # 栈空时截取完整JSON
70 json_str = text[start_index:i + 1]
71 try:
72 orjson.loads(json_str) # 验证有效性
73 results.append(json_str)
74 except:
75 pass
76 else:
77 stack = [] # 括号不匹配则重置
78 if len(results) > 0 and results[0]:
79 return results[0]
80 return None
81
82def string_to_numeric_hash(text: str, bits: Optional[int] = 64) -> int:
83 hash_bytes = hashlib.sha256(text.encode()).digest()

Callers 7

select_datasourceMethod · 0.90
check_sqlMethod · 0.90
check_save_chartMethod · 0.90

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected