Common JSON Errors and How to Fix Them
Learn how to identify, understand, and fix JSON errors with practical examples and browser-based debugging tools.
To fix JSON errors, first validate the JSON using a validator, format it for better readability, identify the reported syntax issue, correct one error at a time, and validate again. Common problems include missing commas, incorrect quotation marks, trailing commas, invalid escape characters, and mismatched braces.
1. What Makes JSON Invalid?
JSON (JavaScript Object Notation) is a lightweight data-interchange format defined by the strict rules of RFC 8259. Because it is designed to be easily read by machines, it has a very strict syntax. A single missing comma or unquoted key makes the entire JSON payload invalid, causing parsers to fail.
A valid JSON structure consists of keys and values, arrays, strings, numbers, booleans, and null values. Keys must always be enclosed in double quotes. Let's look at a simple valid JSON object:
{
"name": "John",
"age": 25,
"active": true
}
In this example, every key is wrapped in double quotes, key-value pairs are separated by commas, and the final item does not end with a trailing comma. Adhering to these strict rules prevents parser failures in your applications.
2. Why JSON Errors Happen
JSON syntax errors usually happen during manual editing, copy-pasting code, or due to bad API configurations. Let's look at the most common causes of JSON errors:
- Manual Editing (40%): Forgetting commas, using single quotes, or adding comments when editing config files manually.
- API Configuration Mistakes (25%): Systems concatenating strings manually instead of using standard JSON encoding libraries.
- Formatting Issues (20%): Copy-pasting raw payloads across editors that convert standard quotes to curved "smart" quotes.
- Encoding Failures (15%): Mismatched character sets that corrupt special characters in keys or string values.
3. 25 Common JSON Errors
Missing Comma Between Key-Value Pairs
Why it fails: The parser expects a comma as a separator between object properties. Without it, the parser fails, reading the next key as an invalid syntax token.
Prevention Tip: Use a JSON editor that auto-inserts commas or run your payload through a formatter to highlight missing separators.
Trailing Comma After the Last Element
Why it fails: A trailing comma tells the parser that another item follows. When the parser finds a closing brace instead, it fails.
Prevention Tip: Remember that JSON does not support trailing commas, unlike standard JavaScript arrays or objects.
Using Single Quotes Instead of Double Quotes
Why it fails: The RFC 8259 specification requires keys and string values to be wrapped in double quotes. Single quotes will trigger parse errors.
Prevention Tip: Use an automated formatter tool to replace single quotes with double quotes automatically.
Unquoted Object Keys
Why it fails: While JavaScript allows unquoted keys, JSON requires all keys to be strings wrapped in double quotes.
Prevention Tip: Always wrap keys in double quotes, even when writing simple config files.
Extra Closing Curly Brace
Why it fails: The parser finishes reading the object at the first closing brace. The second brace is read as extra text outside the document, triggering an error.
Prevention Tip: Use a JSON formatter to check that all braces and brackets match correctly.
Missing Closing Curly Brace
Why it fails: The parser reaches the end of the file before finding the closing brace, triggering an 'unexpected end of input' error.
Prevention Tip: Enable brace-matching in your code editor to make sure all opened braces are closed.
Missing Closing Square Bracket for Arrays
Why it fails: The parser reaches the end of the file before finding the closing bracket of the array.
Prevention Tip: Always format arrays with proper indentation to easily check that the closing bracket is present.
Extra Closing Square Bracket
Why it fails: The parser reads the second closing bracket as invalid text outside the array scope.
Prevention Tip: Run your payload through a formatter to check that all braces and brackets match.
Unescaped Control Characters in Strings
Why it fails: Backward slashes (\) are used for escape sequences (like \n or \t). An unescaped backslash causes the parser to fail.
Prevention Tip: Always use double backslashes (\\) to escape backward slashes in strings.
Double Commas Separating Fields
Why it fails: The parser reads the second comma as an empty or missing element, triggering a syntax error.
Prevention Tip: Avoid manual editing mistakes by using a formatter to clean up duplicate characters.
Missing Colon Separator
Why it fails: The colon (:) is required to separate keys from their values. Without it, parsing fails.
Prevention Tip: Check that every key is followed by a colon before its value.
Invalid Case for Boolean Values
Why it fails: JSON specifies booleans must be lowercase (`true` or `false`). Uppercase variants (like Python's `True`) trigger errors.
Prevention Tip: Always write booleans in lowercase when working with JSON.
Using Undefined Values
Why it fails: JSON does not support `undefined`. Use `null` to represent empty or missing values.
Prevention Tip: Replace `undefined` values with `null` when preparing data payloads.
Using NaN (Not a Number)
Why it fails: The JSON standard only supports real numbers. Using `NaN` will trigger parse errors.
Prevention Tip: Convert `NaN` values to `null` before encoding your data to JSON.
Using Infinity Values
Why it fails: JSON does not support `Infinity`. Use numeric limits or `null` instead.
Prevention Tip: Check that your numerical values are real numbers before parsing.
Using Comments Inside JSON Files
Why it fails: JSON is a data format and does not support comments (like `//` or `/* */`). Adding them makes the JSON invalid.
Prevention Tip: Keep comments in your documentation, not in your JSON configuration files.
Duplicate Keys in the Same Object
Why it fails: While some parsers ignore duplicate keys and keep the last value, others will fail, leading to inconsistent behavior in your apps.
Prevention Tip: Check that all keys within the same object are unique.
Invalid Unicode Characters
Why it fails: JSON requires Unicode escape sequences to follow the `\uXXXX` format. Hex formats like `\x` will trigger errors.
Prevention Tip: Use valid Unicode characters or escape sequences in your strings.
Improper Nesting of Objects and Arrays
Why it fails: Key-value pairs must reside in objects `{}`. Placing them directly inside arrays `[]` is invalid.
Prevention Tip: Check your nesting structure to ensure objects and arrays are used correctly.
Mixed Array Item Separators
Why it fails: JSON arrays must use commas as item separators. Semicolons (;) are not supported.
Prevention Tip: Always use commas to separate items in an array.
Using Curved "Smart" Quotes
Why it fails: Copying text from some text editors can convert straight double quotes to curved "smart" quotes, which the parser reads as invalid characters.
Prevention Tip: Write code in a text editor designed for programming, and avoid rich text editors.
Extra Characters After the JSON Block
Why it fails: The parser expects the document to end after the closing brace of the main object. Extra text outside this block triggers an error.
Prevention Tip: Keep only the JSON block in your file, and remove any extra code.
Malformed Array Declarations
Why it fails: Mismatched or duplicate commas inside an array create empty elements, causing the parser to fail.
Prevention Tip: Check your arrays for extra commas before parsing.
Broken API Response Returning HTML
Why it fails: APIs that experience internal server errors can return HTML pages instead of the expected JSON data, triggering a 'unexpected token <' error.
Prevention Tip: Check the response headers and HTTP status codes before parsing the data payload.
Incomplete JSON Payload
Why it fails: Network interruptions or truncated API logs can leave documents unfinished, resulting in mismatched braces.
Prevention Tip: Verify that the entire payload has downloaded successfully before parsing.
4. Step-by-Step JSON Debugging Workflow
Avoid guessing when fixing JSON errors. Follow this structured debugging process to identify and resolve issues quickly:
Step 1: Copy the Original JSON
Always work on a copy of your JSON payload. Keep a backup of the original data in a separate file to prevent accidental data loss during debugging.
Step 2: Validate the JSON
Paste the copy into our local JSON Validator. This runs syntax checks in your browser memory to highlight errors without uploading your data to remote servers.
Step 3: Beautify the JSON
Run your JSON through a Formatter or Beautifier. Indentation and line breaks make missing commas, unquoted keys, and mismatched braces much easier to locate.
Step 4: Identify the Exact Error
Review the error message from the validator. Common parser messages include:
- Unexpected token: Usually indicates single quotes or missing commas right before the highlighted character.
- Unexpected end of JSON input: Typically caused by a missing closing brace or bracket.
- Expected property name: Unquoted keys or trailing commas before closing braces.
- Invalid character: Mismatched encoding or smart quotes in keys or strings.
Step 5: Correct the Syntax
Fix one error at a time. Make a single edit, run the validator again, and repeat the process until all syntax issues are resolved.
Step 6: Test the JSON
Run a final validation check on the corrected JSON to verify that the payload parses correctly in your application.
5. Debugging API Responses
Many JSON errors originate from APIs rather than manually edited files. Network issues, authentication failures, or server crashes can corrupt data payloads before they reach your app.
If you see a parsing error like `Unexpected token <`, it usually means the server failed and returned an HTML error page (starting with `<!DOCTYPE html>`) instead of the expected JSON data.
Always check the HTTP status code (like 200, 401, or 500) and verify that the `Content-Type` header is set to `application/json` before parsing the response body.
6. JSON Formatter vs JSON Validator
Formatters and validators perform different tasks. Use this comparison table to choose the right tool for your debugging needs:
| Feature | JSON Formatter | JSON Validator |
|---|---|---|
| Beautifies code structure | β Yes | β No | Finds syntax errors | β No | β Yes |
| Adds standard indentation | β Yes | β No |
| Validates RFC 8259 syntax | β No | β Yes |
| Improves code readability | β Yes | β No |
We recommend using both tools together. Format the JSON first to make it readable, and then use the validator to locate and fix syntax issues.
7. IDE vs Browser-Based JSON Tools
Choose between IDE plugins and browser-based tools based on your workflow needs:
| Feature | Browser-Based Tools | IDE Plugins |
|---|---|---|
| Installation required | β No (Instant access) | β Yes (Plugin install) |
| Processing location | π Local memory (100% private) | π» Local computer |
| System impact | β‘ Zero slowdown | β³ Can slow down IDE start times |
| Automatic updates | β Yes | β No (Manual updates) |
Browser-based tools are ideal for quick debugging, working on shared computers, remote work, or when you want to avoid bloated IDE extensions.
8. JSON Validation Checklist
Check these items before parsing or saving your JSON configuration files:
- Quotes Check: Are all keys and string values wrapped in double quotes?
- Commas Check: Are elements separated by commas, with no trailing comma at the end?
- Braces Match: Do all opening braces and brackets have matching closing pairs?
- Value Check: Are you using valid JSON types (no `undefined` or `NaN`)?
- Comments Check: Have you removed all comments from the file?
- Character Encoding: Is the file saved using UTF-8 encoding?
9. Frequently Asked Questions
Standard JSON Code Patterns
Use these standard JSON patterns as references when creating or validating data structures:
1. Valid Object
{
"id": 1,
"name": "Alice",
"active": true
}
A simple object containing numeric, string, and boolean values with correct comma separation.
2. Valid Array
[
{
"id": 1
},
{
"id": 2
}
]
An array of objects with correct comma separation and enclosing square brackets.
3. Nested Object
{
"user": {
"name": "John",
"address": {
"city": "London"
}
}
}
A nested hierarchy where each child object is enclosed in curly braces.
4. API Response
{
"status": "success",
"data": {
"users": [
{
"id": 1,
"name": "Alice"
}
]
}
}
A standard API response object containing status info and nested array arrays.
Featured Free JSON Utilities
Beautify minified JSON blocks and add standard indentation locally.
Use this tool →Check syntax rules and validate your payloads locally in memory.
Use this tool →Remove line breaks and spacing to reduce file sizes for API payloads.
Use this tool →Beautify SQL queries and align syntax keywords locally.
Use this tool →