AllJSONTools

Free JSON Developer Tools

How to Fix Common JSON Errors: A Developer's Debugging Guide

2026-02-26 · 11 min read · By AllJSONTools

JSON
Debugging
Errors
Having JSON issues?

Paste broken JSON and fix it instantly with AI — plain-English explanations included.

Fix JSON with AI

Why JSON Errors Are So Frustrating

JSON looks deceptively simple. It is just keys, values, braces, and brackets — how hard can it be? In practice, a single misplaced comma or a stray single quote can bring an entire application to a halt with an unhelpful error message like Unexpected token or SyntaxError: JSON.parse. The problem is that JSON parsers are strict by design. Unlike JavaScript, JSON has no room for loose syntax, comments, or trailing commas.

This guide covers the nine most common JSON errors developers encounter, shows you exactly what the broken JSON looks like compared to the corrected version, and gives you a systematic debugging workflow so you can fix these problems in seconds. Whether you are debugging an API response, editing a configuration file, or parsing user input, this article has you covered.

Error #1: Trailing Commas

JavaScript happily accepts trailing commas in objects and arrays. JSON does not. A comma after the last element in an array or the last property in an object is a syntax error that will cause every JSON parser to reject the document.

Broken

json
{
  "name": "Alice",
  "age": 30,
  "roles": ["admin", "editor",],
}

Fixed

json
{
  "name": "Alice",
  "age": 30,
  "roles": ["admin", "editor"]
}

How to spot it: Look for a comma immediately before a closing ] or }. This is the single most common JSON syntax error, especially when developers copy-paste from JavaScript code. Paste your JSON into the JSON Formatter to have trailing commas flagged instantly.

Error #2: Single Quotes Instead of Double Quotes

The JSON specification (RFC 8259) mandates that all strings — both keys and values — must be enclosed in double quotes. Single quotes, backticks, and unquoted strings are all invalid. This trips up developers coming from Python (which allows single-quoted strings) or JavaScript (which allows both).

Broken

json
{
  'name': 'Alice',
  'email': 'alice@example.com'
}

Fixed

json
{
  "name": "Alice",
  "email": "alice@example.com"
}

Tip: If you are generating JSON from Python, always use json.dumps() rather than str() or repr(). str() on a Python dictionary produces single-quoted output that is not valid JSON.

Error #3: Unquoted Keys

In JavaScript object literals, keys do not need quotes as long as they are valid identifiers. In JSON, every key must be a double-quoted string — no exceptions.

Broken

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

Fixed

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

Common cause: Copying a JavaScript object directly from source code into a JSON file. If you have a JavaScript object you need to convert, run JSON.stringify(obj, null, 2) in your browser console or use the JSON Formatter to convert it into valid, properly-quoted JSON.

Error #4: Missing or Extra Brackets and Braces

Mismatched brackets and braces are especially common in deeply nested JSON. A missing closing } or an extra ] can produce an error that points to the very end of the document rather than where the actual problem is.

Broken — Missing closing brace on nested object

json
{
  "user": {
    "name": "Alice",
    "address": {
      "city": "Portland",
      "state": "OR"
  },
  "active": true
}

Fixed

json
{
  "user": {
    "name": "Alice",
    "address": {
      "city": "Portland",
      "state": "OR"
    }
  },
  "active": true
}

Debugging tip: The JSON Tree Viewer is ideal for this class of error. It renders JSON as a collapsible tree, so you can visually confirm that every object and array is nested at the correct level. When brackets are mismatched, the tree structure will look obviously wrong.

Error #5: Comments in JSON

JSON does not support comments of any kind. Neither // single-line comments nor /* */ block comments are part of the specification. This is a deliberate design decision by Douglas Crockford, who feared that comments would be misused to add parsing directives and undermine JSON’s simplicity.

Broken

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

Fixed

json
{
  "host": "localhost",
  "port": 5432,
  "database": "myapp"
}

Workarounds: If you need comments in configuration files, consider these alternatives:

  • Use a "_comment" key as a convention: "_comment": "Default PostgreSQL port"

  • Switch to JSONC (JSON with Comments), which tools like VS Code support natively

  • Use a format that supports comments, such as YAML or TOML, for configuration files

Error #6: Undefined, NaN, and Infinity

JavaScript’s undefined, NaN, and Infinity are not valid JSON values. The only valid JSON value types are strings, numbers, booleans, null, objects, and arrays.

Broken

json
{
  "temperature": NaN,
  "maxValue": Infinity,
  "callback": undefined
}

Fixed

json
{
  "temperature": null,
  "maxValue": 1.7976931348623157e+308,
  "callback": null
}

What happens in JavaScript: JSON.stringify() silently converts NaN and Infinity to null, and drops properties whose value is undefined. This can lead to subtle bugs if you are not aware of it. For more on this behavior, see our guide on parsing JSON in JavaScript.

javascript
const obj = { a: NaN, b: Infinity, c: undefined };
console.log(JSON.stringify(obj));
// Output: {"a":null,"b":null}
// Note: "c" is silently dropped

Error #7: Duplicate Keys

The JSON specification says that keys should be unique but does not strictly forbid duplicates. In practice, behavior varies by parser: most JavaScript parsers silently keep the last value, Python’s json.loads() also keeps the last value, and some strict validators will reject the document outright.

Problematic

json
{
  "name": "Alice",
  "role": "admin",
  "role": "editor"
}

In most parsers, the value of role will be "editor" because the second definition overwrites the first. This is almost never intentional.

Fixed

json
{
  "name": "Alice",
  "roles": ["admin", "editor"]
}

Best practice: If a key needs multiple values, use an array. To catch duplicate keys automatically, use the JSON Formatter which will flag duplicate keys during parsing. You can also use the JSON Diff tool to compare a version of your JSON before and after deduplication to see exactly what changed.

Error #8: Incorrect Escape Sequences and Unicode

JSON has a strict set of valid escape sequences. Only these are allowed inside strings: \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX (four hex digits). Anything else is invalid.

Broken — Invalid escape sequences

json
{
  "path": "C:\Users\alice\documents",
  "message": "Line 1\x0ALine 2",
  "tab": "\a"
}

The \\x0A hex escape and \\a are not valid in JSON. The Windows path also fails because \\U, \\a, and \\d are not recognized JSON escape sequences.

Fixed

json
{
  "path": "C:\\Users\\alice\\documents",
  "message": "Line 1\nLine 2",
  "tab": "\t"
}

Key rule: Every backslash in a JSON string must be part of a valid escape sequence. For literal backslashes (like Windows paths), you need to double them: \\. For Unicode characters beyond the basic set, use the \uXXXX format. For example, a copyright symbol is \u00A9.

Error #9: BOM (Byte Order Mark)

A Byte Order Mark (BOM) is an invisible Unicode character ( U+FEFF) that some text editors and Windows applications insert at the beginning of a file. It is invisible in most editors but will cause JSON parsers to fail with an Unexpected token error at position 0.

What it looks like

text
{"name": "Alice"}
^--- invisible BOM character here

The error message is particularly confusing because the JSON looks perfectly valid. You cannot see the BOM in most editors.

How to fix it

javascript
// Strip the BOM before parsing
function stripBOM(text) {
  if (text.charCodeAt(0) === 0xFEFF) {
    return text.slice(1);
  }
  return text;
}

const clean = stripBOM(rawJson);
const data = JSON.parse(clean);

Prevention: Configure your text editor to save files as UTF-8 without BOM. In VS Code, you can check the encoding in the bottom status bar and change it to “UTF-8” (not “UTF-8 with BOM”). When reading files in Node.js, strip the BOM programmatically as shown above.

Systematic Debugging Workflow

When you encounter a JSON parse error, resist the urge to stare at the raw text looking for the problem. Instead, follow this step-by-step workflow to find and fix the issue quickly.

  1. Step 1: Paste into the Formatter. Copy your JSON into the JSON Formatter. It will immediately highlight the line and character position where parsing fails. This catches trailing commas, single quotes, unquoted keys, comments, and most syntax errors in one step.

  2. Step 2: Check the tree view. If the syntax is valid but the structure looks wrong, open the JSON Tree Viewer. Expand each level and confirm the nesting is correct. This is how you catch mismatched brackets and unexpected nesting.

  3. Step 3: Validate the schema. If the JSON parses correctly but your application still rejects it, the problem might be structural rather than syntactic. Use the JSON Schema Validator to check whether the data matches the expected schema — for example, missing required fields, wrong data types, or values outside allowed ranges.

  4. Step 4: Diff against a known-good version. If you have a working version of the JSON, use the JSON Diff tool to compare the broken version against the working one. The diff will highlight exactly which keys or values changed, making it trivial to pinpoint the regression.

  5. Step 5: Check for invisible characters. If everything looks correct but parsing still fails, check for invisible characters like BOM, zero-width spaces, or non-breaking spaces. Copy the first few characters into a hex viewer or use the BOM stripping approach described in Error #9.

For a deeper dive into handling JSON in code, including error handling patterns and type-safe parsing, see our guide on parsing JSON in JavaScript. If you are building APIs that produce or consume JSON, our JSON API best practices article covers validation, error responses, and schema design.

Summary: Quick Reference Table

Bookmark this table for the next time you hit a JSON parse error. Each row links the error to its fix in one glance.

ErrorExampleFix
Trailing comma{"a": 1,}Remove the comma before the closing brace or bracket
Single quotes{'a': 1}Replace all single quotes with double quotes
Unquoted keys{a: 1}Wrap all keys in double quotes
Mismatched brackets{"a": [1, 2}Ensure every opening bracket/brace has a matching close
Comments// commentRemove all comments or switch to JSONC
undefined / NaN / Infinity{"a": NaN}Replace with null or a valid number
Duplicate keys{"a": 1, "a": 2}Remove duplicates or merge values into an array
Bad escape sequences\x0AUse valid JSON escapes: \n, \uXXXX
BOM characterU+FEFF at startSave file as UTF-8 without BOM; strip BOM in code

Every one of these errors can be caught instantly by pasting your JSON into the JSON Formatter. For structural issues that go beyond syntax, validate against a schema with the JSON Schema Validator. And when you need to compare two versions of a document side by side, the JSON Diff tool will highlight every difference.

Having JSON issues?

Paste broken JSON and fix it instantly with AI — plain-English explanations included.

Fix JSON with AI