File size: 5,920 Bytes
a55ea57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
File: /cleanup.py
Project: utils
Created Date: Saturday January 31st 2026
Author: Christian Nonis <alch.infoemail@gmail.com>
-----
Last Modified: Thursday February 19th 2026 7:45:12 pm
Modified By: Christian Nonis <alch.infoemail@gmail.com>
-----
"""

import json
import re


def _repair_array_json(s: str) -> str:
    s = s.replace(',"{', ",{")
    s = re.sub(r'\}{2,}\s*,\s*"', '},"', s)
    return s


def _repair_trailing_commas(s: str) -> str:
    s = re.sub(r',\s*]', ']', s)
    s = re.sub(r',\s*}', '}', s)
    return s


def _extract_json_array_after_key(text: str, key: str):
    pattern = re.escape(key) + r"\s*:\s*\["
    match = re.search(pattern, text)
    if not match:
        return None
    start = match.end() - 1
    depth = 0
    i = start
    while i < len(text):
        if text[i] == "[":
            depth += 1
        elif text[i] == "]":
            depth -= 1
            if depth == 0:
                substring = text[start : i + 1]
                try:
                    return json.loads(substring)
                except Exception:
                    try:
                        return json.loads(_repair_array_json(substring))
                    except Exception:
                        return None
        i += 1
    return None


def strip_properties(
    objs: list[dict], pop_also: list[str] | None = None
) -> list[dict]:
    if pop_also is None:
        pop_also = []
    cleaned_objs = []
    for obj in objs:
        if not isinstance(obj, dict):
            cleaned_objs.append(obj)
            continue
        cleaned_obj = obj.copy()

        keys_to_pop = []
        for key, value in cleaned_obj.items():
            if key in pop_also:
                keys_to_pop.append(key)
                continue

            # Python's bool type are subclasses of int, False is 0 and True is 1.
            # This can lead to unexpected behavior if '0' or '1' are used as keys.
            if isinstance(key, bool):
                continue

            if isinstance(value, dict):
                if len(value.keys()) == 0:
                    keys_to_pop.append(key)
                else:
                    cleaned_obj[key] = strip_properties([value], pop_also)[0]
            elif isinstance(value, list):
                if len(value) == 0:
                    keys_to_pop.append(key)
                else:
                    new_list = []
                    for item in value:
                        if isinstance(item, dict):
                            new_list.append(strip_properties([item], pop_also)[0])
                        else:
                            new_list.append(item)
                    cleaned_obj[key] = new_list
            elif isinstance(value, str):
                if len(value.strip()) == 0:
                    keys_to_pop.append(key)
            elif value is None:
                keys_to_pop.append(key)

        for key in keys_to_pop:
            cleaned_obj.pop(key)

        cleaned_objs.append(cleaned_obj)

    return cleaned_objs


def _last_json_object(text: str) -> dict:
    if not text or "{" not in text:
        return {}
    start = text.rfind("{")
    depth = 0
    for i in range(start, len(text)):
        if text[i] == "{":
            depth += 1
        elif text[i] == "}":
            depth -= 1
            if depth == 0:
                try:
                    return json.loads(text[start : i + 1])
                except Exception:
                    try:
                        return json.loads(_repair_array_json(text[start : i + 1]))
                    except Exception:
                        pass
                return {}
    return {}


def strip_json(text: str) -> dict:
    if not text:
        return {}
    if not isinstance(text, str):
        return {}
    raw = text.strip().replace("```json", "").replace("```", "").strip()
    try:
        parsed = json.loads(raw)
        if isinstance(parsed, dict):
            return parsed
        if isinstance(parsed, list):
            return {"entities": parsed}
    except Exception:
        pass
    try:
        repaired = _repair_array_json(raw)
        parsed = json.loads(repaired)
        if isinstance(parsed, dict):
            return parsed
        if isinstance(parsed, list):
            return {"entities": parsed}
    except Exception:
        pass
    try:
        repaired = _repair_trailing_commas(raw)
        parsed = json.loads(repaired)
        if isinstance(parsed, dict):
            return parsed
        if isinstance(parsed, list):
            return {"entities": parsed}
    except Exception:
        pass
    if raw.strip().startswith("["):
        depth = 0
        start = raw.index("[")
        end = start
        for i in range(start, len(raw)):
            if raw[i] == "[":
                depth += 1
            elif raw[i] == "]":
                depth -= 1
                if depth == 0:
                    end = i + 1
                    break
        if end > start:
            substring = raw[start:end]
            for repair in (lambda s: s, _repair_trailing_commas, _repair_array_json):
                try:
                    parsed = json.loads(repair(substring))
                    if isinstance(parsed, list):
                        return {"entities": parsed}
                except Exception:
                    continue
    obj_match = re.search(r"\{.*\}", raw, flags=re.DOTALL)
    if obj_match:
        try:
            return json.loads(obj_match.group(0))
        except Exception:
            try:
                return json.loads(_repair_array_json(obj_match.group(0)))
            except Exception:
                pass
    entities = _extract_json_array_after_key(raw, "entities")
    if entities is not None:
        return {"entities": entities}
    last = _last_json_object(raw)
    if last and isinstance(last.get("entities"), list):
        return last
    return {}