jupiter0913 commited on
Commit
4c1f298
·
1 Parent(s): 95b9695

feature(#35): fix json parsing issue for alarm.

Browse files
Files changed (1) hide show
  1. 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
- result = result.replace("{'", '{"')
64
- result = result.replace("'}", '"}')
65
- result = result.replace("': '", '": "')
66
- result = result.replace("': \\\"", '": \"')
67
- result = result.replace("', '", '", "')
68
- result = result.replace("':", '":')
 
 
 
 
69
 
70
- substring = '\\"}'
71
- replacement = '\"}'
72
 
73
- index = result.rfind(substring)
74
 
75
- if index == len(result) - 3:
76
- result = result[:index] + replacement + result[index + len(substring):]
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)