Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 924 Bytes
928f123 |
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 |
import json
def find_json_snippet(raw_snippet):
json_parsed_string = None
json_start_index = raw_snippet.find('{')
json_end_index = raw_snippet.rfind('}')
if json_start_index >= 0 and json_end_index >= 0:
json_snippet = raw_snippet[json_start_index:json_end_index+1]
try:
json_parsed_string = json.loads(json_snippet, strict=False)
except:
raise ValueError('......failed to parse string into JSON format')
else:
raise ValueError('......No JSON code snippet found in string.')
return json_parsed_string
def parse_first_json_snippet(snippet):
json_parsed_string = None
if isinstance(snippet, list):
for snippet_piece in snippet:
try:
json_parsed_string = find_json_snippet(snippet_piece)
return json_parsed_string
except:
pass
else:
try:
json_parsed_string = find_json_snippet(snippet)
except Exception as e:
print(e)
raise ValueError()
return json_parsed_string |