JSON Repair Tool
Automatically fix common JSON issues. Perfect for cleaning up LLM outputs, copy-pasted code, or hand-edited configuration files.
Common Issues We Fix
{"a": 1,}→{"a": 1}{'a': 1}→{"a": 1}{a: 1}→{"a": 1}{"a": 1 "b": 2}→{"a": 1, "b": 2}{"a": 1} // note→{"a": 1}{"a": [1, 2→{"a": [1, 2]}What is JSON Repair?
JSON Repair automatically fixes common syntax errors that make JSON invalid. Unlike a validator that just tells you something is wrong, this tool actually fixes the problems and gives you working JSON.
This is especially useful when working with:
- LLM/AI outputs — ChatGPT, Claude, and other models sometimes return malformed JSON
- Copy-pasted code — JSON copied from JavaScript often has trailing commas or single quotes
- Hand-edited configs — Easy to forget a comma or add an extra one
- API responses — Truncated or corrupted responses from network issues
Issues We Automatically Fix
Trailing Commas
JavaScript allows trailing commas, but JSON doesn't. We remove them automatically:
// Before (invalid)
{
"name": "John",
"age": 30, ← trailing comma
}
// After (valid)
{
"name": "John",
"age": 30
}Single Quotes
JSON requires double quotes. We convert single quotes to double:
// Before (invalid)
{'name': 'John'}
// After (valid)
{"name": "John"}Unquoted Keys
JavaScript object keys can be unquoted, but JSON requires quotes:
// Before (invalid)
{name: "John", age: 30}
// After (valid)
{"name": "John", "age": 30}Comments
JSON doesn't support comments, but many config files include them. We strip them out:
// Before (invalid)
{
"debug": true, // enable debug mode
/* timeout in ms */
"timeout": 5000
}
// After (valid)
{
"debug": true,
"timeout": 5000
}Markdown Code Blocks
LLMs often wrap JSON in markdown code blocks. We extract the JSON:
// Before (invalid)
```json
{"name": "John"}
```
// After (valid)
{"name": "John"}Truncated JSON
When JSON is cut off mid-stream (common with LLM outputs), we add missing brackets:
// Before (truncated)
{"users": [{"name": "John"}, {"name": "Jane"
// After (completed)
{"users": [{"name": "John"}, {"name": "Jane"}]}Missing Commas
We detect and add missing commas between elements:
// Before (invalid)
{"a": 1 "b": 2 "c": 3}
// After (valid)
{"a": 1, "b": 2, "c": 3}Working with LLM Outputs
Large Language Models like ChatGPT, Claude, and GPT-4 are incredibly useful for generating structured data, but they sometimes produce invalid JSON. Common issues include:
- Wrapping JSON in markdown code blocks (
```json ... ```) - Adding explanatory text before or after the JSON
- Truncating output mid-object due to token limits
- Using JavaScript-style syntax instead of strict JSON
This repair tool handles all of these cases, making it perfect for AI/LLM workflows.
Programmatic JSON Repair
Need to repair JSON in code? Here are some approaches:
JavaScript/TypeScript
// Using the jsonrepair library (what this tool uses)
import { jsonrepair } from 'jsonrepair';
const broken = "{'name': 'John', 'age': 30,}";
const fixed = jsonrepair(broken);
console.log(fixed); // {"name": "John", "age": 30}
// Parse the repaired JSON
const data = JSON.parse(fixed);Python
# Using json-repair library
from json_repair import repair_json
broken = "{'name': 'John', 'age': 30,}"
fixed = repair_json(broken)
print(fixed) # {"name": "John", "age": 30}When NOT to Use Repair
JSON repair is great for quick fixes, but be careful:
- Don't use for security-critical data — The repair might change the meaning of the data in unexpected ways
- Always validate the result — Make sure the repaired JSON has the structure you expect
- Fix the source — If you're repeatedly repairing JSON from the same source, fix the source instead
JSON5 Alternative
If you're writing configuration files and want a more lenient syntax, consider using JSON5. It's a superset of JSON that allows:
- Comments (single and multi-line)
- Trailing commas
- Unquoted keys
- Single quotes
- Multi-line strings
We also have a JSON5 to JSON converter if you need to convert JSON5 to standard JSON.
Related Tools
- JSON Validator — Validate JSON and see detailed error messages
- JSON Pretty Print — Format repaired JSON nicely
- JSON Minify — Compress JSON after repair
- JSON Escape — Escape special characters in strings
Frequently Asked Questions
Can this fix any broken JSON?
Not always. The tool can fix common syntax issues, but if the data structure itself is corrupted (wrong values, missing fields), it can't know what the correct data should be. It fixes syntax, not semantics.
Is the repaired JSON guaranteed to be correct?
The repaired JSON will be valid JSON (parseable), but you should always verify that the data structure matches what you expect. The repair process makes its best guess about what you intended.
Why do LLMs produce broken JSON?
LLMs are trained on text that includes both valid JSON and JavaScript object literals. They sometimes mix the two syntaxes. Additionally, token limits can cause truncation, and the model might add explanatory text around the JSON.
Can I use this in production?
For quick fixes and development, absolutely. For production systems, consider using the jsonrepair npm package directly with proper error handling and validation.