-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_fixer.py
64 lines (61 loc) · 1.92 KB
/
json_fixer.py
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
import json
import config
def try_add_missing_quotes(rsp):
# rsp almost valid, but no quotes on the keys!
#example:
# {
# bot_name: "Document Creator Bot",
# command_name: "Create Document",
# message_to_user: "What type of document would you like to create? Text, Image, or Spreadsheet?",
# document_type: None
# }
new_rsp = ""
split_by_space = rsp.split(" ")
for x in split_by_space:
if x.endswith(':'):
x = f'"{x}":'
new_rsp += x + " "
return new_rsp
def force_to_json(rsp, property_name):
try:
if config.is_debug:
print("RSP: ")
print(rsp)
print("")
json.loads(rsp, strict=False)
return rsp
except Exception as e1:
if config.is_debug:
print(e1)
print("RSP: ")
print(rsp)
try:
rsp = rsp.replace("Output:", "")
if config.is_debug:
print(rsp)
json.loads(rsp, strict=False)
return rsp
except Exception as e1:
if config.is_debug:
print(e1)
print("RSP: ")
print(rsp)
try:
rsp = try_add_missing_quotes(rsp)
if config.is_debug:
print(rsp)
json.loads(rsp, strict=False)
return rsp
except Exception:
if not ":" in rsp: # not already an attempt at JSON
dict = {
property_name: rsp
}
return json.dumps(dict)
else: # an attempt at JSON, but not valid JSON!
dict = {
property_name: "Cannot answer this question",
"error": "Invalid JSON",
"original": rsp
}
return json.dumps(dict)