How to Fix Corrupted JSON Files

04 May 2026 1,385 words

How to Fix Corrupted JSON Files

Corrupted JSON files are frustrating but often fixable. Whether you are a developer trying to parse an API response, a data analyst working with exported data, or a hobbyist tinkering with configuration files, encountering a broken JSON file can halt your progress. Fortunately, most JSON corruption follows predictable patterns, and with the right tools and techniques, you can repair the damage quickly. This guide walks you through common JSON errors, diagnostic steps, repair strategies, and prevention tips so you can handle corrupted JSON files with confidence.

Common JSON Errors

Understanding the most frequent JSON syntax errors is the first step to fixing them. JSON has strict formatting rules, and even a single misplaced character can break the entire file. Here are the most common issues you will encounter:

1. Missing Commas Between Array Elements or Object Properties

In JSON, every key-value pair in an object must be followed by a comma except for the last one. The same rule applies to elements in an array. Forgetting a comma is one of the most frequent mistakes human editors make. For example:

{
  "name": "Alice"
  "age": 30
}

The missing comma after the "name" line will cause a parse error. The correct version should have a comma between the two properties.

2. Trailing Commas After the Last Element

Many programming languages allow trailing commas in objects and arrays, but JSON strictly forbids them. A trailing comma looks like this:

{
  "name": "Alice",
  "age": 30,
}

The comma after 30 is illegal. JSON parsers will throw an error at this position. Always double-check that the last item in any object or array does not have a trailing comma.

3. Unbalanced Brackets and Braces

Every opening bracket [ must have a matching closing bracket ], and every opening brace { must have a matching closing brace }. When these are unbalanced, the JSON parser will reach the end of the file without finding the expected closing character. This often happens when you delete or add sections of a JSON file manually without carefully matching every opening symbol.

4. Missing Quotes Around Property Names

In JSON, all property names must be enclosed in double quotes. Unlike JavaScript, JSON does not allow unquoted keys. This is one of the most common sources of corruption when someone writes JSON by hand:

{
  name: "Alice"
}

The key name must be wrapped in double quotes: "name". Without them, the parser will treat name as an identifier and fail.

5. Unescaped Characters in Strings

JSON strings must escape certain characters using a backslash. The characters that require escaping include double quotes (\"), backslashes (\\), forward slashes (\/), newlines (\n), tabs (\t), carriage returns (\r), form feeds (\f), and backspaces (\b). If you include an unescaped double quote inside a string, the parser will think the string has ended prematurely.

{
  "message": "He said, "Hello!""
}

This will fail because the double quotes around Hello! break the string. The correct version is:

{
  "message": "He said, \"Hello!\""
}

6. Single Quotes Instead of Double Quotes

JSON only supports double quotes for strings and property names. Single quotes are not valid. If you copy JavaScript object syntax into a JSON file, you might accidentally use single quotes:

{
  'name': 'Alice'
}

Both the key and value use single quotes, which is invalid JSON. Replace all single quotes with double quotes.

Diagnostic Steps

Before you can fix a corrupted JSON file, you need to pinpoint exactly where the error is. Follow these diagnostic steps:

1. Validate with a JSON Validator Tool

The quickest way to identify corruption is to paste your JSON into a JSON Validator tool. These tools parse your JSON and provide detailed error messages, including the exact line number and character position where the parser failed. This narrows your search to a small section of the file instead of forcing you to scan the entire document manually.

2. Check the Error Message for Line Numbers

Most JSON parsers include a line number and column number in their error output. For example, an error might say Unexpected token '}' at line 42, column 13. This tells you exactly where the parser got confused. However, note that the reported position is often slightly after the actual error because the parser only detects the problem when it encounters an unexpected token.

3. Review the Area Around the Reported Error

Once you have a line number, examine the surrounding 5 to 10 lines for any of the common errors listed above. Pay special attention to:

  • The line immediately before the error line (the actual mistake might be there)
  • Any recent edits you made to the file
  • Copy-pasted content that might have introduced invisible characters

4. Look for Common Patterns

Train yourself to spot common error patterns quickly:

  • Scan for missing commas between adjacent key-value pairs
  • Check that every { has a corresponding } and every [ has a ]
  • Look for unquoted property names
  • Check for trailing commas at the end of objects and arrays
  • Ensure strings do not contain unescaped control characters

Using the JSON Formatter for Repair

The JSON Formatter tool is not just for beautifying JSON. It also highlights syntax errors with color-coded indicators and provides suggestions for common issues. When you paste corrupted JSON into the formatter:

  1. It attempts to parse your JSON and reports the first error it encounters
  2. It highlights the problematic section in red
  3. It shows the expected token versus the actual token at the failure point

This visual feedback makes it much easier to understand what the parser expects versus what you provided. Use the formatter iteratively: fix one error, re-paste, fix the next error, and repeat until the JSON validates successfully.

Advanced Repair Techniques

Using a JSON Repair Library

For severely corrupted JSON, consider using a JSON repair library. These libraries use heuristics to fix common errors automatically. They can add missing commas, remove trailing commas, add missing quotes, balance brackets, and even fix unescaped characters. Popular options include:

  • json-repair (Node.js)
  • demjson (Python)
  • json-sanitizer (Java)

Manual Repair for Large Files

When dealing with large JSON files (thousands of lines), manual repair can be tedious. Use these strategies:

  • Divide the file into smaller chunks and validate each chunk separately
  • Use a diff tool to compare against a known-good version
  • Write a simple script that parses the JSON line by line and reports structural issues

Prevention Tips

The best way to deal with corrupted JSON is to prevent it from happening in the first place. Adopt these practices:

Always Validate JSON Before Saving

Make validation a habit. Most code editors have JSON validation plugins, or you can run your JSON through a command-line validator like jq . file.json before saving. A one-second check can save you hours of debugging later.

Use Linting in Your Editor

Configure your editor to highlight JSON syntax errors in real time. VS Code, Sublime Text, and JetBrains IDEs all have built-in JSON validation that underlines errors as you type. This catches mistakes immediately instead of letting them accumulate.

Escape Special Characters Properly

Whenever you include user-generated content or special characters in JSON strings, ensure they are properly escaped. Use a library function like JSON.stringify() instead of building JSON strings manually by concatenating text.

Be Careful with Manual Edits to Large JSON Files

Manual editing is the single biggest source of JSON corruption. When you must edit a large JSON file by hand:

  • Use an editor with bracket matching and syntax highlighting
  • Make small changes and validate frequently
  • Keep a backup of the original file before making edits
  • Consider using a JSON-aware editor that understands the structure

Conclusion

Corrupted JSON files are a common but solvable problem. By understanding the typical errors, using validation tools effectively, and adopting preventive practices, you can minimize downtime and keep your data flowing. The JSON Validator and JSON Formatter tools on Help2Code are excellent first-line resources for diagnosing and fixing corruption. Bookmark them for your next JSON troubleshooting session.


About this article

Learn how to diagnose and fix corrupted JSON files using online tools and debugging techniques.

Help2Code Logo
Menu