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
You can create a Python function called `fix_json` that handles the specified issues with broken JSON strings. Below is a sample implementation that addresses the problems you mentioned:
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 | import json import re def fix_json(broken_json_str): # Fix incorrect parentheses to square brackets # Convert tuples (a, b) to lists [a, b] broken_json_str = re.sub(r'\((.*?)\)', r'[\1]', broken_json_str) # Fix single quotes to double quotes # This regex replaces single quotes with double quotes broken_json_str = re.sub(r"'", r'"', broken_json_str) # Fix True/False to true/false broken_json_str = broken_json_str.replace('True', 'true').replace('False', 'false') # Parse the fixed JSON string try: parsed_json = json.loads(broken_json_str) return parsed_json except json.JSONDecodeError as e: raise ValueError(f"Fixed JSON string could not be parsed: {e}") # Example usage: broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json_str): # Fix incorrect parentheses to square brackets # Convert tuples (a, b) to lists [a, b] broken_json_str = re.sub(r'\((.*?)\)', r'[\1]', broken_json_str) # Fix single quotes to double quotes # This regex replaces single quotes with double quotes broken_json_str = re.sub(r"'", r'"', broken_json_str) # Fix True/False to true/false broken_json_str = broken_json_str.replace('True', 'true').replace('False', 'false') # Parse the fixed JSON string try: parsed_json = json.loads(broken_json_str) return parsed_json except json.JSONDecodeError as e: raise ValueError(f"Fixed JSON string could not be parsed: {e}") # Example usage: broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json_str): # Fix incorrect parentheses to square brackets # Convert tuples (a, b) to lists [a, b] broken_json_str = re.sub(r'\((.*?)\)', r'[\1]', broken_json_str) # Fix single quotes to double quotes # This regex replaces single quotes with double quotes broken_json_str = re.sub(r"'", r'"', broken_json_str) # Fix True/False to true/false broken_json_str = broken_json_str.replace('True', 'true').replace('False', 'false') # Parse the fixed JSON string try: parsed_json = json.loads(broken_json_str) return parsed_json except json.JSONDecodeError as e: raise ValueError(f"Fixed JSON string could not be parsed: {e}") # Example usage: broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} 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') |
{'a': [5, 4], 'b': True}
Traceback (most recent call last):
File "/usr/src/app/main.py", line 19, in fix_json
parsed_json = json.loads(broken_json_str)
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 35 (char 34)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/app/main.py", line 41, in <module>
answer = fix_json('''{'True': False, 'b()': (3, 4), "c'": []}''')
File "/usr/src/app/main.py", line 22, in fix_json
raise ValueError(f"Fixed JSON string could not be parsed: {e}")
ValueError: Fixed JSON string could not be parsed: Expecting ':' delimiter: line 1 column 35 (char 34)
Testing if the previous output contains the string All tests passed
: False
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
You can create a function called `fix_json` that addresses the issues you've listed by using regular expressions and string manipulation. Below is a sample implementation of this function:
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 | import json import re def fix_json(broken_json): # Fix incorrect parentheses by replacing them with square brackets fixed_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Fix single quotes by replacing them with double quotes fixed_json = fixed_json.replace("'", '"') # Fix boolean values by replacing 'False'/'True' with 'false'/'true' fixed_json = fixed_json.replace('False', 'false').replace('True', 'true') # Optionally, remove any trailing commas which are not allowed in JSON fixed_json = re.sub(r',\s*}', '}', fixed_json) fixed_json = re.sub(r',\s*]', ']', fixed_json) try: # Parse the fixed JSON string into a Python object parsed_json = json.loads(fixed_json) return parsed_json except json.JSONDecodeError as e: raise ValueError(f"Failed to parse JSON: {e}") # Example usage if __name__ == "__main__": broken_json = "{'a': (5, 4), 'b': True, 'c': False}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true, 'c': false} |
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 | import json import re def fix_json(broken_json): # Fix incorrect parentheses by replacing them with square brackets fixed_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Fix single quotes by replacing them with double quotes fixed_json = fixed_json.replace("'", '"') # Fix boolean values by replacing 'False'/'True' with 'false'/'true' fixed_json = fixed_json.replace('False', 'false').replace('True', 'true') # Optionally, remove any trailing commas which are not allowed in JSON fixed_json = re.sub(r',\s*}', '}', fixed_json) fixed_json = re.sub(r',\s*]', ']', fixed_json) try: # Parse the fixed JSON string into a Python object parsed_json = json.loads(fixed_json) return parsed_json except json.JSONDecodeError as e: raise ValueError(f"Failed to parse JSON: {e}") # Example usage if __name__ == "__main__": broken_json = "{'a': (5, 4), 'b': True, 'c': False}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true, 'c': false} |
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 | import json import re def fix_json(broken_json): # Fix incorrect parentheses by replacing them with square brackets fixed_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Fix single quotes by replacing them with double quotes fixed_json = fixed_json.replace("'", '"') # Fix boolean values by replacing 'False'/'True' with 'false'/'true' fixed_json = fixed_json.replace('False', 'false').replace('True', 'true') # Optionally, remove any trailing commas which are not allowed in JSON fixed_json = re.sub(r',\s*}', '}', fixed_json) fixed_json = re.sub(r',\s*]', ']', fixed_json) try: # Parse the fixed JSON string into a Python object parsed_json = json.loads(fixed_json) return parsed_json except json.JSONDecodeError as e: raise ValueError(f"Failed to parse JSON: {e}") # Example usage if __name__ == "__main__": broken_json = "{'a': (5, 4), 'b': True, 'c': False}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true, 'c': false} 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') |
{'a': [5, 4], 'b': True, 'c': False}
Traceback (most recent call last):
File "/usr/src/app/main.py", line 21, in fix_json
parsed_json = json.loads(fixed_json)
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 35 (char 34)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/app/main.py", line 44, in <module>
answer = fix_json('''{'True': False, 'b()': (3, 4), "c'": []}''')
File "/usr/src/app/main.py", line 24, in fix_json
raise ValueError(f"Failed to parse JSON: {e}")
ValueError: Failed to parse JSON: Expecting ':' delimiter: line 1 column 35 (char 34)
Testing if the previous output contains the string All tests passed
: False
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
You can create a function called `fix_json` that makes use of regular expressions to address the various issues you've mentioned with the JSON string. Below is a Python implementation of the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import json import re def fix_json(broken_json: str) -> dict: # Fix incorrect parentheses to brackets broken_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Fix single quotes to double quotes broken_json = broken_json.replace("'", '"') # Fix boolean values broken_json = broken_json.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string return json.loads(broken_json) except json.JSONDecodeError as e: raise ValueError(f"Failed to decode JSON: {e}") # Example usage if __name__ == "__main__": broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json: str) -> dict: # Fix incorrect parentheses to brackets broken_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Fix single quotes to double quotes broken_json = broken_json.replace("'", '"') # Fix boolean values broken_json = broken_json.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string return json.loads(broken_json) except json.JSONDecodeError as e: raise ValueError(f"Failed to decode JSON: {e}") # Example usage if __name__ == "__main__": broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json: str) -> dict: # Fix incorrect parentheses to brackets broken_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Fix single quotes to double quotes broken_json = broken_json.replace("'", '"') # Fix boolean values broken_json = broken_json.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string return json.loads(broken_json) except json.JSONDecodeError as e: raise ValueError(f"Failed to decode JSON: {e}") # Example usage if __name__ == "__main__": broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} 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') |
{'a': [5, 4], 'b': True}
Traceback (most recent call last):
File "/usr/src/app/main.py", line 17, in fix_json
return json.loads(broken_json)
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 35 (char 34)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/app/main.py", line 39, in <module>
answer = fix_json('''{'True': False, 'b()': (3, 4), "c'": []}''')
File "/usr/src/app/main.py", line 19, in fix_json
raise ValueError(f"Failed to decode JSON: {e}")
ValueError: Failed to decode JSON: Expecting ':' delimiter: line 1 column 35 (char 34)
Testing if the previous output contains the string All tests passed
: False
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
Certainly! Below is a Python function called `fix_json` that addresses the issues you've mentioned for fixing broken JSON objects. The function will handle incorrect parentheses, incorrect single quotes, and the capitalization of boolean literals. It uses regular expressions and string manipulation to make the necessary corrections before parsing the JSON string.
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 | import json import re def fix_json(broken_json_str): # Fix incorrect parentheses by replacing them with brackets fixed_json_str = re.sub(r'\((.*?)\)', r'[\1]', broken_json_str) # Fix incorrect single quotes to double quotes fixed_json_str = fixed_json_str.replace("'", '"') # Fix boolean literals fixed_json_str = fixed_json_str.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string parsed_json = json.loads(fixed_json_str) except json.JSONDecodeError as e: print(f"Failed to decode JSON: {e}") return None return parsed_json # Example usage broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json_str): # Fix incorrect parentheses by replacing them with brackets fixed_json_str = re.sub(r'\((.*?)\)', r'[\1]', broken_json_str) # Fix incorrect single quotes to double quotes fixed_json_str = fixed_json_str.replace("'", '"') # Fix boolean literals fixed_json_str = fixed_json_str.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string parsed_json = json.loads(fixed_json_str) except json.JSONDecodeError as e: print(f"Failed to decode JSON: {e}") return None return parsed_json # Example usage broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json_str): # Fix incorrect parentheses by replacing them with brackets fixed_json_str = re.sub(r'\((.*?)\)', r'[\1]', broken_json_str) # Fix incorrect single quotes to double quotes fixed_json_str = fixed_json_str.replace("'", '"') # Fix boolean literals fixed_json_str = fixed_json_str.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string parsed_json = json.loads(fixed_json_str) except json.JSONDecodeError as e: print(f"Failed to decode JSON: {e}") return None return parsed_json # Example usage broken_json = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json) print(fixed_json) # Output: {'a': [5, 4], 'b': true} 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') |
{'a': [5, 4], 'b': True}
Failed to decode JSON: Expecting ':' delimiter: line 1 column 35 (char 34)
Traceback (most recent call last):
File "/usr/src/app/main.py", line 43, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got None instead of {'True': False, 'b()': [3, 4], "c'": []}
Testing if the previous output contains the string All tests passed
: False
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
You can create a function called `fix_json` that addresses the specified issues in the JSON string. The function will utilize regular expressions to replace incorrect patterns and then parse the fixed JSON string using Python's built-in `json` module. Here's how you can implement it:
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 | import json import re def fix_json(broken_json: str): # Replace incorrect parentheses with square brackets fixed_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Replace single quotes with double quotes fixed_json = fixed_json.replace("'", '"') # Replace Python's True/False with JSON's true/false fixed_json = fixed_json.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string parsed_json = json.loads(fixed_json) return parsed_json except json.JSONDecodeError as e: print(f"Error decoding JSON: {e}") return None # Example usage broken_json_str = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json_str) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json: str): # Replace incorrect parentheses with square brackets fixed_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Replace single quotes with double quotes fixed_json = fixed_json.replace("'", '"') # Replace Python's True/False with JSON's true/false fixed_json = fixed_json.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string parsed_json = json.loads(fixed_json) return parsed_json except json.JSONDecodeError as e: print(f"Error decoding JSON: {e}") return None # Example usage broken_json_str = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json_str) print(fixed_json) # Output: {'a': [5, 4], 'b': true} |
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 | import json import re def fix_json(broken_json: str): # Replace incorrect parentheses with square brackets fixed_json = re.sub(r'\((.*?)\)', r'[\1]', broken_json) # Replace single quotes with double quotes fixed_json = fixed_json.replace("'", '"') # Replace Python's True/False with JSON's true/false fixed_json = fixed_json.replace('True', 'true').replace('False', 'false') try: # Parse the fixed JSON string parsed_json = json.loads(fixed_json) return parsed_json except json.JSONDecodeError as e: print(f"Error decoding JSON: {e}") return None # Example usage broken_json_str = "{'a': (5, 4), 'b': True}" fixed_json = fix_json(broken_json_str) print(fixed_json) # Output: {'a': [5, 4], 'b': true} 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') |
{'a': [5, 4], 'b': True}
Error decoding JSON: Expecting ':' delimiter: line 1 column 35 (char 34)
Traceback (most recent call last):
File "/usr/src/app/main.py", line 42, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
AssertionError: Wrong answer; got None instead of {'True': False, 'b()': [3, 4], "c'": []}
Testing if the previous output contains the string All tests passed
: False