-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_helpers.py
More file actions
30 lines (25 loc) · 849 Bytes
/
json_helpers.py
File metadata and controls
30 lines (25 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
import json
def extract_json(text_response):
pattern = r'\{.*?\}'
matches = re.finditer(pattern, text_response, re.DOTALL)
json_objects = []
for match in matches:
json_str = extend_search_new(text_response, match.span())
try:
json_obj = json.loads(json_str)
json_objects.append(json_obj)
except json.JSONDecodeError:
continue
return json_objects if json_objects else None
def extend_search_new(text, span):
start, end = span
nest_count = 1 # Starts with 1 since we know '{' is at the start position
for i in range(end, len(text)):
if text[i] == '{':
nest_count += 1
elif text[i] == '}':
nest_count -= 1
if nest_count == 0:
return text[start:i+1]
return text[start:end]