Skip to content

Commit 48a199c

Browse files
JinpengMiaometa-codesync[bot]
authored andcommitted
Fix response parsing errors by updating extract_json
Reviewed By: Kravchie Differential Revision: D85703824 fbshipit-source-id: 36c29e8128aa600b4bff0ae680eabd3c3e28d30c
1 parent b21ee27 commit 48a199c

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

CybersecurityBenchmarks/benchmark/benchmark_utils.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,51 @@ def get_mime_type(file_path: str) -> Optional[str]:
117117

118118

119119
def extract_json(text: str) -> Union[Dict[str, Any], bool]:
120-
text = text.replace("\n", "")
120+
"""Extracts the first complete JSON object from text."""
121+
if not text or not text.strip():
122+
print("Error extracting JSON: Empty input text")
123+
return False
124+
125+
# Try parsing entire text first
126+
try:
127+
return json.loads(text)
128+
except json.JSONDecodeError:
129+
pass
130+
131+
# Find first complete JSON object by tracking balanced braces
121132
try:
122-
start = text.rindex("{")
123-
end = text.rindex("}")
124-
return json.loads(text[start : end + 1])
125-
except Exception as e:
133+
start = text.index("{")
134+
brace_count = 0
135+
in_string = False
136+
escape_next = False
137+
138+
for i in range(start, len(text)):
139+
char = text[i]
140+
141+
if escape_next:
142+
escape_next = False
143+
continue
144+
145+
if char == "\\":
146+
escape_next = True
147+
continue
148+
149+
if char == '"':
150+
in_string = not in_string
151+
continue
152+
153+
if not in_string:
154+
if char == "{":
155+
brace_count += 1
156+
elif char == "}":
157+
brace_count -= 1
158+
if brace_count == 0:
159+
return json.loads(text[start : i + 1])
160+
161+
print("Error extracting JSON: No balanced braces found")
162+
return False
163+
164+
except (ValueError, json.JSONDecodeError) as e:
126165
print(f"Error extracting JSON: {e}")
127166
return False
128167

0 commit comments

Comments
 (0)