Common JSON Errors and How to Fix Them

JSON Formatter Hub

Common JSON Errors and How to Fix Them

Working with JSON can be frustrating when you encounter errors. Even experienced developers face JSON parsing errors, syntax issues, and validation problems. This comprehensive guide will help you identify and fix the most common JSON errors, saving you time and frustration during development and debugging.

This guide is designed for developers, testers, and students who work with JSON regularly. You'll learn to recognize common error messages, understand what causes them, and fix them quickly. We'll cover syntax errors, validation issues, parsing problems, and best practices to prevent errors in the first place.

Whether you're debugging API responses, fixing configuration files, or troubleshooting data exchange issues, understanding common JSON errors is essential. By the end of this guide, you'll be able to identify and resolve JSON errors confidently and efficiently.

Understanding JSON Error Messages

JSON parsers provide error messages when they encounter invalid JSON. These messages typically include the error type and the position where the error occurred. Common error messages include "Unexpected token", "Expected property name", "Unterminated string", and "Trailing comma". Understanding these messages is the first step to fixing JSON errors.

Most JSON parsers will stop at the first error they encounter, so fixing one error might reveal additional errors. It's important to fix errors in order, starting from the beginning of the JSON structure. Always validate your JSON after making changes to ensure all errors are resolved.

Most Common JSON Errors

1. Trailing Comma Error

One of the most common JSON errors is the trailing comma. JSON does not allow commas after the last element in an object or array, even though JavaScript allows this syntax.

Error Example:

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

Error Message: "Unexpected token }" or "Expected property name"

Fix: Remove the trailing comma after the last property:

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

This error also occurs in arrays:

["apple", "banana", "orange",]  ❌
["apple", "banana", "orange"]   âś…

2. Single Quotes Instead of Double Quotes

JSON requires double quotes for strings and keys. Single quotes are not valid in JSON, even though they work in JavaScript.

Error Example:

{'name': 'John', 'age': 30}

Error Message: "Unexpected token" or "Expected property name"

Fix: Replace all single quotes with double quotes:

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

3. Unescaped Special Characters

Special characters in JSON strings must be escaped. This includes quotes, backslashes, newlines, and other control characters.

Error Example:

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

Error Message: "Unexpected token" or "Unterminated string"

Fix: Escape the inner quotes with backslashes:

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

Other characters that need escaping:

4. Missing Quotes Around Keys

All keys in JSON objects must be strings enclosed in double quotes. Unquoted keys are not valid in JSON.

Error Example:

{name: "John", age: 30}

Error Message: "Expected property name" or "Unexpected token"

Fix: Add double quotes around all keys:

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

5. Comments in JSON

JSON does not support comments. You cannot use // for single-line comments or /* */ for multi-line comments in JSON files.

Error Example:

{
  // This is a comment
  "name": "John",
  /* Another comment */
  "age": 30
}

Error Message: "Unexpected token" or "Expected property name"

Fix: Remove all comments. If you need to document JSON, use a separate documentation file or include the information in the data itself:

{
  "name": "John",
  "age": 30,
  "_comment": "User profile data"
}

6. Invalid Number Format

JSON numbers must be valid numeric values. Leading zeros, hexadecimal numbers, and other non-standard formats are not allowed.

Error Examples:

{"id": 0123}        ❌ Leading zero
{"id": 0x1A}        ❌ Hexadecimal
{"id": 1.2.3}       ❌ Multiple decimal points
{"id": +42}         ❌ Plus sign prefix

Fix: Use valid numeric formats:

{"id": 123}         âś…
{"id": 26}          âś… (decimal equivalent of 0x1A)
{"id": 1.23}        âś…
{"id": 42}          âś…

7. Missing Commas

Commas are required to separate elements in objects and arrays. Missing commas cause parsing errors.

Error Example:

{
  "name": "John"
  "age": 30
}

Error Message: "Expected ',' or '}'"

Fix: Add commas between properties:

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

8. Mismatched Brackets or Braces

Every opening bracket [ or brace { must have a corresponding closing bracket ] or brace }. Mismatched brackets cause parsing errors.

Error Example:

{
  "users": [
    {"name": "John", "age": 30}
  }
}

Error Message: "Expected ',' or ']'" or "Unexpected token"

Fix: Ensure all brackets and braces are properly matched:

{
  "users": [
    {"name": "John", "age": 30}
  ]
}

9. Undefined or NaN Values

JSON does not support JavaScript's undefined or NaN values. These must be replaced with null or removed.

Error Example:

{"value": undefined, "result": NaN}

Error Message: "Unexpected token" or "Unexpected identifier"

Fix: Use null instead:

{"value": null, "result": null}

10. Control Characters

Unescaped control characters (like newlines, tabs, or other non-printable characters) in strings can cause parsing errors.

Error Example:

{"message": "Line 1
Line 2"}

Fix: Escape control characters or use \n for newlines:

{"message": "Line 1\nLine 2"}

How to Debug JSON Errors

When you encounter a JSON error, follow these steps to identify and fix it:

Step 1: Validate Your JSON

Use a JSON validator to identify syntax errors. Our free JSON validator will highlight errors and show you exactly where the problem is located.

Step 2: Check Error Messages

Read the error message carefully. It usually tells you what token was expected and where the error occurred. Look at the line number or position mentioned in the error.

Step 3: Check Common Issues

Review the common errors listed above. Most JSON errors fall into one of these categories. Check for trailing commas, single quotes, missing quotes, and other common mistakes.

Step 4: Format Your JSON

Formatting JSON with proper indentation makes it easier to spot errors. Use our JSON formatter to automatically format your JSON, which often reveals structural issues.

Step 5: Validate Again

After making fixes, validate your JSON again to ensure all errors are resolved. Sometimes fixing one error reveals additional errors that were hidden.

Best Practices to Prevent JSON Errors

Following best practices can help you avoid JSON errors in the first place:

How Our Tool Helps

Fixing JSON errors manually can be time-consuming and frustrating. Our free JSON Formatter and Validator makes it easy to identify and fix JSON errors:

Whether you're debugging API responses, fixing configuration files, or learning JSON syntax, our tool helps you identify and resolve errors quickly. Simply paste your JSON, and our validator will show you all syntax errors with clear explanations.

For more information on JSON fundamentals, read our guide on what JSON is. To learn about proper formatting, check out our guide on how to format JSON correctly. If you need to compare two JSON files to find differences, use our JSON Compare tool.

Conclusion

JSON errors are common, but they're also easy to fix once you understand what causes them. The most common errors—trailing commas, single quotes, missing quotes, and unescaped characters—can be quickly identified and resolved with the right tools and knowledge.

Remember to always validate your JSON before using it in production. Use proper formatting to make errors easier to spot, and follow best practices to prevent errors from occurring in the first place. With practice and the right tools, you'll be able to fix JSON errors quickly and confidently.

Don't waste time manually debugging JSON errors. Use our free JSON Validator to instantly identify and fix syntax errors. Our tool processes everything in your browser, ensuring your data remains completely private and secure.