Write me a python function called fix_json that fixes broken JSON objects, and returns the parsed object. I'll pass the JSON object as a string and it should fix: - Incorect parenthesis, e.g., {"a": (5, 4)} -> {"a": [5, 4]} - Incorrect single quotes, e.g., {'a': 5} -> {"a": 5} - False/True -> false/true
Write me a python function called fix_json that fixes broken JSON objects, and returns the parsed object. I'll pass the JSON object as a string and it should fix: - Incorect parenthesis, e.g., {"a": (5, 4)} -> {"a": [5, 4]} - Incorrect single quotes, e.g., {'a': 5} -> {"a": 5} - False/True -> false/true
To solve this problem, we need to write a Python function called `fix_json` that corrects common issues in broken JSON strings. The function should handle three specific problems: incorrect use of parentheses for arrays, single quotes instead of double quotes, and incorrect casing for boolean values. ### Approach The approach involves three main steps to address each of the issues mentioned: 1. **Replace Single Quotes with Double Quotes**: Convert single-quoted strings to double-quoted strings, ensuring any internal single quotes are properly unescaped and internal double quotes are escaped. 2. **Correct Boolean Values**: Replace Python-style boolean values `True` and `False` with their JSON counterparts `true` and `false`, ensuring these replacements only occur outside of string literals. 3. **Convert Parentheses to Brackets**: Replace parentheses `()` with square brackets `[]` to denote arrays, ensuring these replacements only occur outside of string literals. ### Solution Code
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 | import re import json def fix_json(json_str): # Step 1: Replace single-quoted strings with double-quoted ones def replace_single_quotes(match): content = match.group(1) content = content.replace("\\'", "'") # Unescape escaped single quotes content = content.replace('"', '\\"') # Escape double quotes within the string return f'"{content}"' json_str = re.sub(r"'((?:[^'\\]|\\.)*)'", replace_single_quotes, json_str) # Step 2: Replace True/False with true/false in non-string parts parts = re.split(r'("[^"\\]*(?:\\.[^"\\]*)*")', json_str) for i in range(len(parts)): if i % 2 == 0: # Non-string parts parts[i] = re.sub(r'\bTrue\b', 'true', parts[i]) parts[i] = re.sub(r'\bFalse\b', 'false', parts[i]) json_str = ''.join(parts) # Step 3: Replace parentheses with brackets in non-string parts parts = re.split(r'("[^"\\]*(?:\\.[^"\\]*)*")', json_str) for i in range(len(parts)): if i % 2 == 0: # Non-string parts parts[i] = parts[i].replace('(', '[').replace(')', ']') json_str = ''.join(parts) # Parse the corrected JSON return json.loads(json_str) |
I extracted the following code from that output:
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 | import re import json def fix_json(json_str): # Step 1: Replace single-quoted strings with double-quoted ones def replace_single_quotes(match): content = match.group(1) content = content.replace("\\'", "'") # Unescape escaped single quotes content = content.replace('"', '\\"') # Escape double quotes within the string return f'"{content}"' json_str = re.sub(r"'((?:[^'\\]|\\.)*)'", replace_single_quotes, json_str) # Step 2: Replace True/False with true/false in non-string parts parts = re.split(r'("[^"\\]*(?:\\.[^"\\]*)*")', json_str) for i in range(len(parts)): if i % 2 == 0: # Non-string parts parts[i] = re.sub(r'\bTrue\b', 'true', parts[i]) parts[i] = re.sub(r'\bFalse\b', 'false', parts[i]) json_str = ''.join(parts) # Step 3: Replace parentheses with brackets in non-string parts parts = re.split(r'("[^"\\]*(?:\\.[^"\\]*)*")', json_str) for i in range(len(parts)): if i % 2 == 0: # Non-string parts parts[i] = parts[i].replace('(', '[').replace(')', ']') json_str = ''.join(parts) # Parse the corrected JSON return json.loads(json_str) |
Running the following program:
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 | import re import json def fix_json(json_str): # Step 1: Replace single-quoted strings with double-quoted ones def replace_single_quotes(match): content = match.group(1) content = content.replace("\\'", "'") # Unescape escaped single quotes content = content.replace('"', '\\"') # Escape double quotes within the string return f'"{content}"' json_str = re.sub(r"'((?:[^'\\]|\\.)*)'", replace_single_quotes, json_str) # Step 2: Replace True/False with true/false in non-string parts parts = re.split(r'("[^"\\]*(?:\\.[^"\\]*)*")', json_str) for i in range(len(parts)): if i % 2 == 0: # Non-string parts parts[i] = re.sub(r'\bTrue\b', 'true', parts[i]) parts[i] = re.sub(r'\bFalse\b', 'false', parts[i]) json_str = ''.join(parts) # Step 3: Replace parentheses with brackets in non-string parts parts = re.split(r'("[^"\\]*(?:\\.[^"\\]*)*")', json_str) for i in range(len(parts)): if i % 2 == 0: # Non-string parts parts[i] = parts[i].replace('(', '[').replace(')', ']') json_str = ''.join(parts) # Parse the corrected JSON return json.loads(json_str) answer = fix_json('''{"a": 5, "b": [3, 4]}''') expected = {"a": 5, "b": [3, 4]} assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = fix_json('''{'a': 5.0, 'b': (True, 4)}''') expected = {"a": 5.0, "b": [True, 4]} assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = fix_json('''{'True': False, 'b()': (3, 4), "c'": []}''') expected = {"True": False, "b()": [3, 4], "c'": []} assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
All tests passed
Testing if the previous output contains the string All tests passed
: True