Advertisement
Troubleshooting Guide

10 Most Common JSON Errors and How to Fix Them

Master JSON syntax by learning to identify and fix the most frequent errors that trip up developers. Complete with examples, solutions, and prevention tips.

Quick Reference: JSON Syntax Rules

✅ Valid JSON

  • • Double quotes for strings and keys
  • • No trailing commas
  • • Proper number formats
  • • Balanced brackets and braces

❌ Invalid JSON

  • • Single quotes or unquoted keys
  • • Comments (// or /* */)
  • • undefined values
  • • Functions or complex objects
1
Trailing Commas

Extra commas before closing brackets or braces

high priority

❌ Invalid JSON

{
  "name": "John",
  "age": 30,
}
Error: Unexpected token } in JSON at position 25

✅ Valid JSON

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

💡 Solution

Remove the trailing comma after the last property or array element.

Prevention Tips:
  • Use a JSON formatter to automatically detect trailing commas
  • Enable linting in your code editor to catch these errors
  • Be extra careful when copying/pasting JSON objects
2
Unquoted Object Keys

Object keys must be wrapped in double quotes

high priority

❌ Invalid JSON

{
  name: "John",
  age: 30
}
Error: Unexpected token n in JSON at position 2

✅ Valid JSON

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

💡 Solution

Always wrap object keys in double quotes, even if they look like valid identifiers.

Prevention Tips:
  • JSON is stricter than JavaScript object notation
  • Use a JSON validator to catch unquoted keys
  • Remember: JSON keys are always strings
3
Single Quotes Instead of Double Quotes

JSON only accepts double quotes for strings

high priority

❌ Invalid JSON

{
  'name': 'John',
  'age': 30
}
Error: Unexpected token ' in JSON at position 2

✅ Valid JSON

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

💡 Solution

Replace all single quotes with double quotes for both keys and string values.

Prevention Tips:
  • Use find/replace to convert single quotes to double quotes
  • Be careful with escaped quotes inside strings
  • JSON parsers are strict about quote types
Advertisement
4
Missing Closing Brackets/Braces

Unclosed arrays or objects cause parsing errors

medium priority

❌ Invalid JSON

{
  "users": [
    {"name": "John"},
    {"name": "Jane"}
  "total": 2
}
Error: Unexpected token 't' in JSON at position 45

✅ Valid JSON

{
  "users": [
    {"name": "John"},
    {"name": "Jane"}
  ],
  "total": 2
}

💡 Solution

Ensure every opening bracket [ has a closing bracket ] and every opening brace { has a closing brace }.

Prevention Tips:
  • Use proper indentation to visualize structure
  • Count opening and closing brackets/braces
  • Use a code editor with bracket matching
5
Invalid Escape Sequences

Incorrect use of backslashes in strings

medium priority

❌ Invalid JSON

{
  "path": "C:\Users\John\Documents",
  "regex": "\d+"
}
Error: Unexpected token in JSON at position 15

✅ Valid JSON

{
  "path": "C:/Users/John/Documents",
  "regex": "\\d+"
}

💡 Solution

Properly escape backslashes by doubling them (\\) or use forward slashes where appropriate.

Prevention Tips:
  • Common escape sequences: \n, \t, \r, \", \\
  • Use raw strings in your programming language when building JSON
  • Test escape sequences in a JSON validator
6
Undefined or Null Without Quotes

JavaScript undefined is not valid JSON

medium priority

❌ Invalid JSON

{
  "name": "John",
  "middleName": undefined,
  "age": null
}
Error: Unexpected token u in JSON at position 25

✅ Valid JSON

{
  "name": "John",
  "middleName": null,
  "age": null
}

💡 Solution

Use null for empty values, or omit the property entirely. Never use undefined in JSON.

Prevention Tips:
  • JSON only supports: string, number, boolean, null, object, array
  • undefined is a JavaScript concept, not JSON
  • Consider omitting properties instead of setting them to null
Advertisement
7
Comments in JSON

JSON doesn't support comments

low priority

❌ Invalid JSON

{
  // User information
  "name": "John",
  /* Age in years */
  "age": 30
}
Error: Unexpected token / in JSON at position 2

✅ Valid JSON

{
  "name": "John",
  "age": 30,
  "_comment": "User information with age in years"
}

💡 Solution

Remove comments or use a special property like '_comment' for documentation.

Prevention Tips:
  • Use JSONC (JSON with Comments) for configuration files if supported
  • Document your JSON schema separately
  • Consider using a more flexible format like YAML for config files
8
Duplicate Object Keys

Multiple properties with the same key name

low priority

❌ Invalid JSON

{
  "name": "John",
  "age": 30,
  "name": "Jane"
}
Error: No error, but last value overwrites previous ones

✅ Valid JSON

{
  "firstName": "John",
  "age": 30,
  "lastName": "Jane"
}

💡 Solution

Ensure all object keys are unique within the same object.

Prevention Tips:
  • JSON parsers typically use the last occurrence
  • Use a linter to detect duplicate keys
  • Consider using arrays for multiple similar values
9
Invalid Number Formats

Incorrectly formatted numeric values

medium priority

❌ Invalid JSON

{
  "price": 19.99.0,
  "count": 01,
  "percentage": .5
}
Error: Unexpected token . in JSON at position 15

✅ Valid JSON

{
  "price": 19.99,
  "count": 1,
  "percentage": 0.5
}

💡 Solution

Follow JSON number format rules: no leading zeros, decimal point needs digits on both sides.

Prevention Tips:
  • Valid: 123, -123, 1.23, -1.23, 1.23e10
  • Invalid: 01, .5, 1., 1.23.45
  • Use parseFloat() to ensure proper number formatting
10
Incorrect Array Syntax

Missing commas or brackets in arrays

medium priority

❌ Invalid JSON

{
  "colors": ["red" "blue", "green"],
  "numbers": [1, 2, 3,]
}
Error: Unexpected token 'b' in JSON at position 18

✅ Valid JSON

{
  "colors": ["red", "blue", "green"],
  "numbers": [1, 2, 3]
}

💡 Solution

Separate array elements with commas and avoid trailing commas.

Prevention Tips:
  • Every array element except the last needs a comma
  • Arrays can contain mixed types in JSON
  • Empty arrays [] are valid
Advertisement

Ready to Fix Your JSON?

Use our free JSON tools to validate, format, and debug your JSON data. Get instant error detection with helpful suggestions.

Related Articles

JSON Best Practices

Learn the essential best practices for writing maintainable and efficient JSON.

Read More
JSON vs XML

Compare JSON and XML to choose the right data format for your project.

Read More
API Testing Guide

Master API testing techniques with JSON validation and debugging tips.

Read More