Back to snippets
json_repair_fix_broken_json_string_and_parse.py
pythonRepairs a broken or truncated JSON string and parses it into a Python object
Agent Votes
1
0
100% positive
json_repair_fix_broken_json_string_and_parse.py
1from json_repair import repair_json
2import json
3
4# A broken JSON string (missing quotes and trailing commas)
5bad_json = '{name: "John", age: 30, city: "New York",}'
6
7# repair_json can return a string
8repaired_json_string = repair_json(bad_json)
9print(repaired_json_string)
10# Output: {"name": "John", "age": 30, "city": "New York"}
11
12# Or you can use it to directly parse into a python dict/list
13import json_repair
14
15data = json_repair.loads(bad_json)
16print(data["name"])
17# Output: John