jupiter0913 commited on
Commit ·
4c1f298
1
Parent(s): 95b9695
feature(#35): fix json parsing issue for alarm.
Browse files- Brain/src/common/utils.py +16 -11
Brain/src/common/utils.py
CHANGED
|
@@ -59,21 +59,26 @@ def validateJSON(jsonData):
|
|
| 59 |
|
| 60 |
def parseJsonFromCompletion(data: str) -> json:
|
| 61 |
result = data[1:-1]
|
|
|
|
| 62 |
# fmt: off
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
# fmt: on
|
| 78 |
try:
|
| 79 |
return json.loads(result)
|
|
|
|
| 59 |
|
| 60 |
def parseJsonFromCompletion(data: str) -> json:
|
| 61 |
result = data[1:-1]
|
| 62 |
+
array = result.split('\\"')
|
| 63 |
# fmt: off
|
| 64 |
+
if len(array) > 1:
|
| 65 |
+
array[0] = array[0].replace("'", '"')
|
| 66 |
+
result = array[0] + '"' + array[1] + '"' + array[2]
|
| 67 |
+
else:
|
| 68 |
+
result = result.replace("{'", '{"')
|
| 69 |
+
result = result.replace("'}", '"}')
|
| 70 |
+
result = result.replace("': '", '": "')
|
| 71 |
+
result = result.replace("': \\\"", '": \"')
|
| 72 |
+
result = result.replace("', '", '", "')
|
| 73 |
+
result = result.replace("':", '":')
|
| 74 |
|
| 75 |
+
substring = '\\"}'
|
| 76 |
+
replacement = '\"}'
|
| 77 |
|
| 78 |
+
index = result.rfind(substring)
|
| 79 |
|
| 80 |
+
if index == len(result) - 3:
|
| 81 |
+
result = result[:index] + replacement + result[index + len(substring):]
|
| 82 |
# fmt: on
|
| 83 |
try:
|
| 84 |
return json.loads(result)
|