โ† Home ยท Blog

10+ Common JSON Errors and How to Fix Them

Published: May 25, 2025 ยท Last updated: June 20, 2025 ยท 11 min read

Every developer has seen the dreaded "Unexpected token" or "JSON.parse error" message. JSON has strict syntax rules, and even a tiny mistake breaks the entire document. This comprehensive guide covers all the common errors, debugging strategies, and tools to help you fix JSON issues quickly.

Error 1: Trailing Commas

The most common JSON error. JavaScript allows trailing commas, but JSON does not. This trips up developers constantly because they're used to JavaScript's more lenient syntax.

Invalid:

{ "name": "Alice", "age": 30, "city": "NYC", // โ† trailing comma - NOT allowed! }

Fixed:

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

Rule: The last item in an object or array must NOT have a comma after it. This applies to both arrays and objects at every nesting level.

Error 2: Single Quotes Instead of Double Quotes

JSON requires double quotes for all strings and keys. Single quotes are completely invalid, even though many programming languages allow them for strings.

Invalid:

{'name': 'Alice', 'city': 'NYC'}

Fixed:

{"name": "Alice", "city": "NYC"}

Rule: Always use double quotes (") for both keys and string values. Never single quotes (') or backticks.

Error 3: Unquoted Keys

In JavaScript objects, keys don't need quotes unless they contain special characters. In JSON, all keys must be double-quoted strings โ€” no exceptions.

Invalid:

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

Fixed:

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

Rule: Every key in JSON must be a double-quoted string, even if it's a simple identifier.

Error 4: Missing Commas Between Properties

Forgetting a comma between key-value pairs is easy to do, especially when editing multi-line JSON manually or when copy-pasting sections.

Invalid:

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

Fixed:

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

Rule: Every key-value pair must be followed by a comma, except the last one. Same applies to array elements.

Error 5: Unmatched Brackets or Braces

Opening a bracket or brace without closing it โ€” or closing one that was never opened. This is especially common in deeply nested structures.

Invalid:

{"users": [{"name": "Alice"}, {"name": "Bob"}}

Fixed:

{"users": [{"name": "Alice"}, {"name": "Bob"}]}

Rule: Every { needs a matching }, and every [ needs a matching ]. Use an editor with bracket matching and highlighting to catch these errors quickly.

Error 6: Invalid Escape Sequences

JSON strings support a limited set of escape sequences. Using unsupported escapes or forgetting to escape special characters causes parsing failures.

Invalid:

{"path": "C:\Users\name"} // backslash not escaped {"message": "Say "hello""} // quotes not escaped

Fixed:

{"path": "C:\\Users\\name"} {"message": "Say \"hello\""}

Valid escape sequences in JSON:

  • \" โ€” double quote
  • \\ โ€” backslash
  • \/ โ€” forward slash (optional)
  • \b โ€” backspace
  • \f โ€” form feed
  • \n โ€” newline
  • \r โ€” carriage return
  • \t โ€” tab
  • \uXXXX โ€” Unicode character (4 hex digits)

Error 7: NaN and Infinity Values

JavaScript's special numeric values NaN, Infinity, and -Infinity are not valid in JSON. They must be represented differently.

Invalid (will fail to parse):

{"result": NaN, "max": Infinity, "min": -Infinity}

Options for fixing:

// Option 1: Use null for missing/invalid values {"result": null, "max": null, "min": null} // Option 2: Use string representations {"result": "NaN", "max": "Infinity", "min": "-Infinity"} // Option 3: Use very large numbers (if appropriate) {"max": 1.7976931348623157e+308}

Note: When using JSON.stringify() in JavaScript, NaN and Infinity are automatically converted to null.

Error 8: Comments in JSON

Standard JSON does not support comments. This surprises many developers who want to document their configuration files.

Invalid:

{ // This is a comment - NOT ALLOWED "name": "Alice", /* Multi-line comments are also invalid */ "age": 30 }

Workarounds:

// Option 1: Use a "_comment" field (ignored by most parsers) { "_comment": "User configuration settings", "name": "Alice", "age": 30 } // Option 2: Use JSON5 or JSONC format (requires special parser) // Option 3: Use YAML instead for config files (supports comments)

Error 9: Duplicate Keys

While technically valid JSON (parsers accept it), duplicate keys cause unpredictable behavior. Different parsers handle duplicates differently โ€” some keep the first value, others keep the last.

Problematic:

{ "name": "Alice", "age": 30, "name": "Bob" // Duplicate key - unpredictable! }

Best practice: Ensure all keys within an object are unique. Our JSON Formatter can detect and warn about duplicate keys.

Error 10: Incorrect Boolean and Null Casing

JSON boolean and null values must be lowercase. Other casings are treated as invalid or as string values.

Invalid:

{ "isActive": True, "isAdmin": FALSE, "middleName": NULL, "verified": TRUE }

Fixed:

{ "isActive": true, "isAdmin": false, "middleName": null, "verified": true }

Rule: Always use lowercase true, false, and null. Python developers: note that Python uses True, False, and None โ€” but JSON requires lowercase.

Error 11: Unicode and Encoding Issues

JSON must be encoded in UTF-8 (per RFC 8259). Character encoding mismatches cause parsing failures or garbled text.

Common symptoms:

  • Replacement characters appearing: {"name": "Caf๏ฟฝ"}
  • Unexpected characters: {"name": "Cafรƒยฉ"}
  • Parse errors on seemingly valid JSON

Solutions:

  • Ensure source files are saved as UTF-8 (without BOM)
  • Set proper Content-Type header: application/json; charset=utf-8
  • Use Unicode escape sequences for problematic characters: {"name": "Caf\u00e9"}
  • Verify database and API connections use UTF-8

Error 12: Numeric Precision Issues

JSON numbers are parsed as IEEE 754 double-precision floating-point numbers in most languages. This causes precision loss for very large integers or precise decimals.

Problem โ€” large ID loses precision:

// Original JSON {"id": 9007199254740993} // After parsing in JavaScript JSON.parse('{"id": 9007199254740993}').id // Returns: 9007199254740992 (wrong!)

Solution โ€” use strings for large numbers:

{"id": "9007199254740993"} {"price": "19.99"} // For exact decimal (financial data)

Safe integer range: Numbers between -(2^53 - 1) and (2^53 - 1) are safe. That's approximately ยฑ9 quadrillion. Beyond that, use strings.

Debugging Strategies: A Systematic Approach

When facing a JSON parsing error, follow this systematic debugging process:

Step 1: Locate the Error Position

Most JSON parsers report the line number and character position where parsing failed. The actual error is usually slightly before this position โ€” often the previous line or a few characters back.

// Error message example: SyntaxError: Unexpected token } in JSON at position 47 // This means: look at character 47 and the characters before it

Step 2: Isolate the Problem

For large JSON documents, use binary search to isolate the problematic section:

  • Copy the first half of the JSON and try parsing it
  • If it parses, the error is in the second half
  • If it fails, the error is in the first half
  • Repeat until you find the specific line

Step 3: Check Common Culprits

At the error location, check for these issues in order:

  • Missing or extra comma on the previous line
  • Unescaped quotes or backslashes in strings
  • Single quotes instead of double quotes
  • Unquoted key names
  • Missing closing bracket or brace
  • Trailing comma before a closing bracket/brace

Step 4: Use a Validator Tool

Paste your JSON into a validation tool that highlights the exact error location. Our JSON Formatter & Validator shows the line number, character position, and highlights the problematic area in red.

IDE Tips: VS Code JSON Extensions

Visual Studio Code provides excellent JSON support out of the box, but these extensions make it even better:

  • JSON Tools โ€” Format, minify, and sort JSON with keyboard shortcuts
  • Paste JSON as Code โ€” Generate TypeScript interfaces from JSON samples
  • JSON Crack โ€” Visualize JSON as interactive graphs
  • JSON Schema Store โ€” Auto-complete and validation for common config files

VS Code built-in features:

  • Format Document (Shift+Alt+F) โ€” auto-formats JSON files
  • Bracket Pair Colorization โ€” visually match brackets (enable in settings)
  • Error Squiggles โ€” red underlines show syntax errors in real-time
  • JSON Schema validation โ€” add "$schema" property for auto-completion

Command-Line JSON Tools

For terminal workflows and automation, these CLI tools are invaluable:

jq โ€” The Swiss Army Knife for JSON

# Validate and format JSON cat data.json | jq . # Check if file is valid JSON (returns 0 or error) jq empty data.json # Pretty-print with 4-space indent jq --indent 4 . data.json # Extract specific field jq '.users[0].name' data.json

Install: brew install jq (Mac) or apt install jq (Linux)

Python's json.tool

# Validate and format (built into Python) python -m json.tool data.json # Pipe from curl curl https://api.example.com/data | python -m json.tool

Node.js One-Liners

# Validate JSON node -e "JSON.parse(require('fs').readFileSync('data.json'))" # Format with 2-space indent node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync('data.json')), null, 2))"

How to Fix JSON Automatically

Don't want to hunt for errors manually? Our Auto-Fix JSON tool detects and repairs many common errors automatically:

  • Removes trailing commas
  • Converts single quotes to double quotes
  • Adds quotes to unquoted keys
  • Fixes boolean/null casing
  • Removes JavaScript-style comments
  • Escapes unescaped characters

Just paste your broken JSON and click "Fix" โ€” the tool shows you exactly what it repaired.

You can also use our JSON Formatter & Validator to pinpoint the exact line and character where an error occurs, making manual fixes straightforward.

Prevention Tips

Avoid JSON errors in the first place with these practices:

  • Use an editor with JSON syntax highlighting (VS Code, Sublime Text, JetBrains IDEs)
  • Enable auto-format on save in your IDE
  • Validate JSON before sending it to an API
  • Use TypeScript or JSON Schema validation to catch errors at compile time
  • Never manually edit minified JSON โ€” format it first, edit, then minify
  • Use JSON.stringify() to generate JSON instead of string concatenation
  • Set up pre-commit hooks to validate JSON config files
  • Use environment-specific configs instead of commenting out JSON sections

More Articles

How to Format JSON โ€” A Complete Beginner's Guide

JSON vs XML vs YAML โ€” When to Use Which Format

How to Compare Two JSON Files

What is JSON โ€” A Complete Guide for Beginners