Fixes broken JSON string objects
Maintained. Last release 10 months ago. Weaker on release cadence.
pip install fix-busted-jsonfix-busted-json
Fix broken json using Python.
For Python 3.6+.
This project fixes broken JSON with the following issues:
- Missing quotes around key names
- Wrong quotes around key names and strings
- Single quotes
- Backticks
- Escaped double quote
- Double escaped double quote
- "Smart" i.e. curly quotes
- Missing commas between key-value pairs and array elements
- Trailing comma after last key-value pair
- Concatenation of string fields
- Replace Python True/False/None with JSON true/false/null
- Remove additional double quote at start of key that gpt-3.5-turbo sometimes adds
- Escape unescaped newline
\nin string value - Deal with many escaping la-la land cases e.g.
{\"res\": \"{ \\\"a\\\": \\\"b\\\" }\"}
Utility functions are also provided for finding JSON objects in text.
https://github.com/Qarj/fix-busted-json
https://pypi.org/project/fix-busted-json
Quickstart
pip install fix-busted-json
Make a file called example_repair_json.py:
#!/usr/bin/env python3
from fix_busted_json import repair_json
invalid_json = "{ name: 'John' 'age': 30, 'city': 'New' + ' York', }"
fixed_json = repair_json(invalid_json)
print(fixed_json)
Note the issues in the invalid JSON:
- name is unquoted
- use of single quotes, JSON spec requires double quotes
- Missing comma
- Concatenation of string fields - not allowed in JSON
- Trailing comma
Run it:
python example_repair_json.py
Output:
{ "name": "John", "age": 30, "city": "New York" }
Why
The project was developed originally to find JSON like objects in log files and pretty print them.
More recently this project has been used to find and then fix broken JSON created by large language models such as gpt-3.5-turbo and gpt-4.
For example a large language model might output a completion like the following: