Skip to content
Debugging

10 JSON Syntax Errors That Waste Time and How to Fix Them

·10 min read
10 JSON Syntax Errors That Waste Time and How to Fix Them - Debugging guide from JSON Prettifier

Quick answer: The most common JSON syntax errors are trailing commas, single quotes instead of double quotes, unquoted keys, mismatched brackets, and comments - none of which JSON allows. The rest of the list is undefined, NaN and Infinity, duplicate keys, top-level scalars in old parsers, and unescaped backslashes or line breaks inside strings. The fix is almost always the same: generate JSON with JSON.stringify() or json.dumps() instead of writing it by hand, and run anything hand-edited through a validator.

You paste a 400-line config file into your application. You hit run. The terminal spits back: SyntaxError: Unexpected token at position 2847. Position 2847. In a minified file. Now you are counting characters like an archaeologist brushing dust off a fossil.

This guide exists so you never have to do that again. These are the ten JSON syntax errors that waste the most developer time - and exactly how to fix each one. (If the message you are staring at is specifically the Unexpected token error or Unexpected end of JSON input, those guides decode the message itself.)

Repair a copy of the failing payload with the JSON Fixer, then review the changes before using the result.

1. Trailing Commas

This is the single most common JSON error. JavaScript allows trailing commas in objects and arrays. JSON does not. Not ever.

Invalid - trailing comma after "editor" and after "true":

{
  "name": "Dana",
  "roles": ["admin", "editor",],
  "active": true,
}

Valid - no trailing commas:

{
  "name": "Dana",
  "roles": ["admin", "editor"],
  "active": true
}

This bites JavaScript developers constantly because every JS linter encourages trailing commas in code. Your muscle memory works against you. Some editors will even auto-add trailing commas in JSON files if you have the wrong formatter extension enabled. Watch for it. The dedicated guide to fixing trailing commas in JSON covers how to spot them in large files and stop them creeping back in.

2. Single Quotes Instead of Double Quotes

JSON requires double quotes for all strings and all keys. Single quotes, backticks, and unquoted strings are all invalid.

Invalid - single quotes:

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

Valid - double quotes only:

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

Python developers hit this often because Python's dict representation uses single quotes by default. If you copy a Python dict from a REPL and try to use it as JSON, it will break. Use json.dumps() to get proper JSON output from Python - the Python json module guide covers the details.

3. Unquoted Keys

JavaScript object literals allow unquoted keys. JSON does not. Every key must be a double-quoted string, no exceptions.

Invalid - unquoted keys:

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

Valid - all keys double-quoted:

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

This happens when people hand-write JSON or copy JavaScript object literals directly. Use JSON.stringify() to convert JavaScript objects to valid JSON rather than writing it by hand. The full list of differences between the two is in JSON vs JavaScript object.

4. Missing or Mismatched Brackets

Every opening brace needs a closing brace. Every opening bracket needs a closing bracket. In deeply nested JSON, it is easy to lose track - especially when editing by hand.

Invalid - missing closing bracket for "tags" array:

{
  "name": "widget",
  "tags": ["ui", "component",
  "version": "2.0"
}

Valid - bracket properly closed:

{
  "name": "widget",
  "tags": ["ui", "component"],
  "version": "2.0"
}

A JSON formatter helps enormously here. Paste the broken JSON into a tool like jsonprettifier.com and the error highlighting will point you directly to the mismatched bracket. Counting brackets manually in a 500-line file is not a good use of your time.

5. Comments

JSON does not support comments. Not single-line with //, not multi-line with /* */, not in any form. This is one of the most frequently requested features and one of the most intentional omissions in the spec.

Invalid - comments are not valid JSON:

{
  // Database configuration
  "host": "localhost",
  "port": 5432, /* default PostgreSQL port */
  "name": "myapp"
}

Valid - no comments:

{
  "host": "localhost",
  "port": 5432,
  "name": "myapp"
}

If you need comments in configuration files, consider JSONC (JSON with Comments, supported by VS Code and TypeScript configs), JSON5, YAML, or TOML - all of which allow comments natively. For standard JSON, the common workaround is a documentation key. We compare all five options in how to add comments in JSON.

{
  "_comment": "Database configuration",
  "host": "localhost",
  "port": 5432,
  "name": "myapp"
}

6. Using undefined

undefined is a JavaScript concept. It does not exist in JSON. When JavaScript's JSON.stringify() encounters undefined, it silently drops the key entirely - which can lead to confusing bugs where data just disappears.

Invalid - undefined is not a JSON value:

{
  "name": "Dana",
  "middleName": undefined,
  "age": 30
}

Valid - use null for missing or empty values:

{
  "name": "Dana",
  "middleName": null,
  "age": 30
}

Similarly, function, Symbol, and other JavaScript-specific types are not valid JSON values. If a value cannot be represented in JSON, use null or omit the key. When to use null and when to omit the key entirely is a design question of its own, covered in JSON null vs undefined.

7. Top-Level Values

Technically, any JSON value type is valid as a top-level document according to RFC 8259. However, older parsers following RFC 4627 only accepted objects and arrays at the top level. This can cause subtle compatibility issues depending on which parser you are using.

// May fail in older parsers
"just a string"

42

true

// Universally safe - wrap in an object or array
{ "value": "just a string" }

[42]

{ "result": true }

In practice, wrapping top-level values in an object is a good habit. It makes the data self-describing and extensible - you can add more fields later without changing the structure.

8. NaN and Infinity

JSON numbers must be finite. NaN, Infinity, and -Infinity are not valid JSON values. JavaScript's JSON.stringify() converts them to null silently, which can mask bugs in your data pipeline.

Invalid - these are not valid JSON numbers:

{
  "result": NaN,
  "limit": Infinity,
  "threshold": -Infinity
}

Valid - use null or a string representation:

{
  "result": null,
  "limit": "Infinity",
  "threshold": "-Infinity"
}

// Or handle with a sentinel value
{
  "result": null,
  "resultError": "NaN - division by zero",
  "limit": 9999999999
}

If you need to represent these special numeric values in a data exchange, the common approach is using string representations or sentinel values, with clear documentation explaining the convention. Very large integers have a related problem - they are valid JSON but lose precision in JavaScript - which JSON numbers, precision, and BigInt explains.

9. Duplicate Keys

The JSON spec says keys "SHOULD be unique" but does not strictly mandate it. Different parsers handle duplicate keys differently - most silently use the last value, some throw errors, and a few keep all duplicates. This inconsistency leads to subtle bugs and even security vulnerabilities.

Problematic - duplicate "status" key:

{
  "id": 1,
  "status": "active",
  "name": "Widget",
  "status": "inactive"
}

What does status equal? It depends on the parser. Most will say "inactive" (last key wins), but do not rely on it.

Valid - unique keys only:

{
  "id": 1,
  "status": "inactive",
  "name": "Widget"
}

Duplicate keys usually happen through copy-paste errors or merge conflicts. A JSON validator will flag them. Some real-world security vulnerabilities have exploited parser differences in handling duplicates, so treating them as errors is the safest approach. A parser-by-parser breakdown is in JSON duplicate keys.

10. Encoding Issues

JSON must be encoded in UTF-8 (RFC 8259 made this mandatory). Problems arise when JSON is saved in other encodings or when special characters are not properly escaped. A byte order mark at the start of a file is a common invisible offender; see JSON encoding, UTF-8, and the BOM.

Invalid - literal control characters and unescaped backslashes:

{
  "message": "Line 1
Line 2",
  "path": "C:\Users\test"
}

Valid - escaped special characters:

{
  "message": "Line 1\nLine 2",
  "path": "C:\\Users\\test"
}

Characters that must be escaped in JSON strings:

  • \" - double quote
  • \\ - backslash
  • \n - newline
  • \r - carriage return
  • \t - tab
  • \uXXXX - Unicode escape for control characters

Windows file paths are a frequent offender because backslashes need to be doubled. If your JSON contains file paths and will not parse, check the backslashes first. The full escape table, surrogate pairs for emoji, and the double-escaping trap are covered in JSON escape characters.

How to Prevent These Errors

Most of these errors share a root cause: writing JSON by hand. The more you can generate JSON programmatically, the fewer syntax errors you will encounter. Use JSON.stringify() in JavaScript, json.dumps() in Python, json.Marshal() in Go, or the equivalent function in your language. These functions produce valid JSON by construction. (JSON produced by a language model is a special case - it fails in its own recognizable ways, catalogued in fixing AI-generated JSON.)

For complex data structures, consider defining a JSON Schema to validate payloads automatically before they reach your application logic. Schema validation catches structural errors - missing fields, wrong types, values out of range - that syntactic validation alone cannot detect.

If you handle JSON in production systems, review our JSON security best practices to prevent injection attacks and data leaks. Security issues and syntax issues often overlap: improperly escaped strings can be both a parse error and a vulnerability.

When you do need to edit JSON manually - updating a config file, crafting a test fixture, debugging a payload - use a validator tool that highlights errors as you type. Catching a trailing comma in your editor is always better than discovering it in a production log at 2 AM.

Frequently Asked Questions

What is the most common JSON syntax error?
Trailing commas. JavaScript allows a comma after the last item in an object or array, JSON does not, and most linters encourage them in code, so muscle memory keeps adding them to JSON files where they cause a parse error.
Can I use single quotes in JSON?
No. JSON requires double quotes for every key and every string value. Single quotes, backticks, and unquoted keys are all invalid. Python developers hit this often because a printed dict uses single quotes; use json.dumps() instead.
Can JSON have comments?
Standard JSON does not allow comments of any kind. If you need them in a config file, use JSONC, JSON5, YAML, or TOML, or add a documentation key such as _comment to a plain JSON object.
Are duplicate keys allowed in JSON?
The spec says keys should be unique but does not forbid duplicates, so parsers disagree: most keep the last value, some throw, a few keep both. Treat duplicate keys as errors; a validator will flag them.
How do I find where my JSON is broken?
Paste it into a JSON validator or formatter. It will point to the exact line and character of the first error, which is far faster than counting characters from a position number in a minified file.

Validate Your JSON Now

Paste a JSON file you are struggling with into our validator. It will highlight the exact line and character where your syntax breaks.

Open JSON Prettifier