File size: 599 Bytes
b0b30af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import json5
import json

def correct_malformed_json(json_string):
    try:
        # Use json5 to parse the more flexible JSON format
        parsed_json = json5.loads(json_string)
        
        # Reformat and pretty-print the JSON using json.dumps
        formatted_json = json.dumps(parsed_json, indent=4)
        
        return formatted_json
    except json5.JSONDecodeError as e:
        return f"Error: {e}"

# Example usage with a malformed JSON string
malformed_json = '{"greeting": ["Good afternoon Tony", "Let me introduce our guest""]}'
print(correct_malformed_json(malformed_json))