How to Quickly Fix Malformed JSON Files
As of May 2026, the fastest way to fix malformed JSON f […]
As of May 2026, the fastest way to fix malformed JSON files is to use automated libraries like json_repair (Python) or jsonrepair (npm). These tools are designed to fix LLM-generated syntax errors instantly. For manual repairs, you’ll need to check for trailing commas, single quotes, or unquoted keys—the most common violations of the RFC 8259 standard.
The Fastest Fix: Using json_repair for LLM and Automated Outputs
Standard parsers, such as Python’s json.loads(), are built to be strict. If they find even one misplaced character, they trigger a JSONDecodeError and stop everything. This is a major headache in 2026 because Large Language Models (LLMs) often wrap JSON in conversational text or cut off responses mid-sentence. These files might be “broken” by technical standards, but the data inside is still valuable.
The most efficient fix is the json_repair library. According to GitHub, this project has grown to over 4,700 stars as of 2026, becoming the go-to tool for handling messy LLM outputs. It works by “guessing” the intent of the string—closing missing brackets, adding quotes, or stripping away the extra text that often surrounds a JSON block.

Python Implementation: From JSONDecodeError to Valid Data
To fix malformed data in your code, first install the library: pip install json-repair. You can then use it just like the standard library.
import json_repair
# Example of a broken string: missing quotes and a truncated value
bad_json = '{"user": "Alice", "status": tru'
decoded_object = json_repair.loads(bad_json)
# Output: {'user': 'Alice', 'status': True}
For more complex tasks, json_repair (v0.59.5+) includes a “Salvage Mode.” As noted in the project documentation, this mode is built specifically for truncated AI responses or corrupted logs. It can force arrays into objects or drop items that are too broken to save, ensuring the final output actually fits your schema. This allows developers to recover data from logs that would normally be deleted as “trash.”
Manual Debugging: Why is your JSON Malformed?
If automation doesn’t work, you have to find exactly where the file breaks the RFC 8259 standard. JSON is much less forgiving than YAML or JavaScript. As the JSONParser Diagnostics Team explains, “The parser fails at the first character it cannot make sense of, which is often a downstream symptom of a problem several lines earlier.”
Common Culprit 1: Are Trailing Commas breaking your file?
According to DEV Community, trailing commas are the #1 cause of parse failures. While they are perfectly fine in JavaScript, a comma after the last item in a JSON array or object is illegal. You must delete any comma that sits immediately before a closing } or ].
Common Culprit 2: Single Quotes vs Double Quotes
JSON requires double quotes (") for both keys and string values. Many Python and JavaScript developers accidentally use single quotes ('), which will cause an immediate error. As TidyCode points out in their 2026 guide, switching these quotes is a mandatory step for a successful conversion.
Common Culprit 3: Unquoted keys
In standard JSON, every key must be wrapped in double quotes. While you can write { name: "Alice" } in YAML or plain JavaScript, it will crash a JSON.parse command.

How to identify the ‘Unexpected Token’ error
When a validator flags an “Unexpected Token,” it means the parser hit a character it didn’t expect to see. This usually happens when the data contains NaN, Infinity, or undefined. Since JSON only supports null, true, false, and specific numbers, you’ll need to replace those JavaScript constants with null or valid numeric values.
Difference Maker: Schema-Guided Repairs vs. Strict Parsing
Deciding whether to use strict parsing or a repair tool depends on where your data is coming from. If you are dealing with a configuration file edited by a human, strict parsing is better because it forces the person to fix their mistakes. But for machine-to-machine data involving LLMs, repair-based parsing is a necessity.
You can also “guide” the repair process using Pydantic v2 or JSON Schema. By giving json_repair a schema to follow, the tool does more than fix syntax; it can fix types (like turning the string "1" into the number 1) and fill in missing required fields with default values.
| Feature | Strict Parsing (json.loads) |
Repair Parsing (json_repair) |
|---|---|---|
| Trailing Commas | Raises JSONDecodeError |
Automatically removed |
| Single Quotes | Fails | Converted to double quotes |
| Truncated Data | Fails | Closes open brackets/quotes |
| Comments | Fails | Automatically stripped |
| Best Use Case | Local Config Files | LLM Outputs / API Logs |
As Stefano Baccianella noted in his 2025 project citation, this approach is optimized for the “mostly correct but technically invalid” JSON that language models tend to produce.
Handling Large-Scale Malformed Data
Repairing a small 10KB snippet is easy, but fixing a multi-gigabyte file requires a strategy that won’t crash your computer. Loading a massive, broken file into memory all at once often leads to Out-of-Memory (OOM) errors.
When to use ijson for streaming repair
For huge datasets, try a streaming parser like ijson. As Scrapfly mentions in their 2026 guide, ijson processes data one piece at a time. While it’s meant for valid JSON, you can combine it with a script that cleans the data line-by-line (removing commas or comments) before it ever hits the parser.
For the most reliable large-scale fix, use a command-line tool like jsonrepair (npm) and save the output directly to a new file:
jsonrepair large_broken.json > fixed.json
This is much more memory-efficient than trying to handle the file in a browser or a Python variable.
Conclusion
Fixing malformed JSON doesn’t have to be a manual chore anymore, thanks to AI-aware libraries like json_repair. While you still need to understand RFC 8259 basics—like the rules against trailing commas, single quotes, and unquoted keys—automation is the only way to handle data at scale in 2026. The best workflow is simple: try an updated repair library first; if that fails, use a validator to pinpoint the specific syntax error. This keeps your applications running even when the data coming in is less than perfect.
FAQ
Can JSON officially support comments or single quotes?
No, the RFC 8259 standard strictly forbids comments. Single quotes are also invalid; only double quotes are allowed for keys and strings. However, modern tools like json_repair can strip comments or convert quotes automatically to make the file parseable by standard libraries.
How do I handle very large malformed JSON files without crashing my memory?
To process multi-gigabyte files, use a streaming parser like ijson to handle the data in chunks. Avoid loading the entire malformed string into a single variable. For the fastest results, use command-line repair tools that pipe the repaired output directly to a new file on your disk.
What is the difference between ‘malformed JSON’ and ‘invalid JSON’?
Malformed JSON violates fundamental syntax rules, such as missing brackets or quotes, making it impossible to parse. Invalid JSON follows all syntax rules but fails to match a specific JSON Schema (e.g., a field is a string when it should be an integer). Fixing malformed JSON is about structural repair; fixing invalid JSON is about data integrity.