DevBolt
·8 min read

How to Validate JSON: The 7 Most Common Errors and How to Fix Them

JSONDebuggingWeb Development

JSON is the backbone of modern APIs, config files, and data exchange. When it breaks, error messages are often cryptic. This guide covers the most common JSON errors, how to spot them, and how to fix them fast.

JSON Syntax Rules

JSON is strict about its syntax. Unlike JavaScript objects, JSON has no room for flexibility. Here are the rules:

  • Keys must be double-quoted strings. "name" is valid, name or 'name' is not.
  • Strings must use double quotes. "hello" works, 'hello' does not.
  • No trailing commas. The last item in an array or object must not have a comma after it.
  • No comments. JSON does not support // or /* */ comments.
  • Values can be: strings, numbers, booleans (true/false), null, arrays, or objects.

The 7 Most Common JSON Errors

1. Trailing Commas

The most frequent mistake. JavaScript allows trailing commas, JSON does not:

Invalid JSON
{
  "name": "Alice",
  "age": 30,    ← trailing comma
}
Valid JSON
{
  "name": "Alice",
  "age": 30
}

2. Single Quotes

JSON requires double quotes for all strings and keys. This is a common mistake when copying from JavaScript or Python code:

Invalid JSON
{'name': 'Alice'}   ← single quotes
Valid JSON
{"name": "Alice"}   ← double quotes

3. Unquoted Keys

JavaScript objects allow unquoted keys. JSON does not:

Invalid JSON
{name: "Alice"}     ← unquoted key
Valid JSON
{"name": "Alice"}   ← quoted key

4. Comments

JSON has no comment syntax. If you need comments in config files, consider JSONC (JSON with Comments, used by VS Code) or YAML:

Invalid JSON
{
  // database config
  "host": "localhost",
  "port": 5432  /* default postgres port */
}

A common workaround is to use a "_comment" key, though this adds to the payload:

Workaround
{
  "_comment": "database config",
  "host": "localhost",
  "port": 5432
}

5. Missing or Extra Brackets

Mismatched {} or [] brackets are hard to spot in deeply nested structures. Most editors highlight matching brackets, and a JSON formatter will immediately show where the mismatch is.

Invalid — missing closing bracket
{
  "users": [
    {"name": "Alice"},
    {"name": "Bob"}

}

6. Invalid Number Formats

JSON numbers have specific rules:

Invalid numbers
.5        ← must have leading zero: 0.5
+1        ← no leading plus sign
0x1A      ← no hex notation
NaN       ← not a valid JSON value
Infinity  ← not a valid JSON value

7. Unescaped Special Characters

Strings containing special characters need proper escaping:

Characters that must be escaped
"   → \"    (double quote)
\   → \\   (backslash)
newline → \n
tab     → \t

A common source of this error is file paths on Windows:

Invalid JSON
{"path": "C:\Users\data"}
Valid JSON
{"path": "C:\\Users\\data"}

How to Debug JSON Errors

  1. 1Use a JSON formatter. Paste your JSON into a JSON Formatter to get an immediate error message with the line number.
  2. 2Check the error position. JSON.parse() errors include a position number. Unexpected token at position 142 means the error is at character 142 of the string.
  3. 3Look at the character before the error. If the parser says "unexpected token at line 5," the actual problem is often on line 4 (a missing comma, for example).
  4. 4Validate programmatically. Wrap JSON.parse() in a try/catch to handle malformed input gracefully.
JavaScript
function parseJSON(input) {
  try {
    return { data: JSON.parse(input), error: null };
  } catch (e) {
    return { data: null, error: e.message };
  }
}

JSON vs JavaScript Objects

Many errors come from confusing JSON with JavaScript object literals. Here's a quick comparison:

FeatureJSONJavaScript
KeysMust be double-quotedCan be unquoted
StringsDouble quotes onlySingle, double, or backticks
Trailing commasNot allowedAllowed
CommentsNot allowedAllowed
FunctionsNot allowedAllowed as values
undefinedNot allowedAllowed

Building APIs that serve JSON?

DigitalOcean App Platform deploys Node.js, Python, and Go apps from a Git repo with zero server config. Auto-scaling, free SSL, and built-in monitoring included.

Validate Your JSON

Use our JSON Formatter & Validator to instantly check and fix your JSON. It pinpoints errors, formats the output with proper indentation, and runs entirely in your browser. For schema validation, try the JSON Schema Validator to verify your JSON matches an expected structure.