# How to fix invalid JSON: common errors and fixes

> JSON gives no second chances: one stray comma or single quote breaks the whole file. The errors everyone hits, why they happen and how to fix each one.

Canonical: https://toolspace.tech/guides/fix-invalid-json/
Last updated: 2026-11-27

You edited a config file, saved an API response, or pasted JSON from somewhere, and the parser refuses it: “Unexpected token”, “Unexpected end of JSON input”, or just a red squiggle in the editor. JSON’s rules are few but absolute, and one stray character breaks the whole document. These are the errors behind almost every failure, and the fix for each one.

## Why JSON is so strict

JSON looks like JavaScript object syntax because that is where it came from, but it is a data format, not a programming language. It allows a small, fixed grammar: double-quoted strings, one number format, no comments, no conveniences. That strictness is the point: a file that parses to the same data in every language. The cost is that things JavaScript forgives, JSON rejects outright.

## Trailing commas

The classic JSON error. A comma after the last item is fine in JavaScript and Python, but JSON expects a value after every comma:

```
{
  "name": "app",
  "version": "1.0.0",
}
```

Remove the comma after `"1.0.0"`. Parsers often point at the `}` or `]` on the next line, which sends people hunting in the wrong place: the real problem is the comma just before it.

## Single quotes

JSON strings must be double-quoted. Single quotes, and backticks from template literals, are not valid JSON:

```
{'name': 'app'}
```

Swap the outer quotes for double quotes: `{"name": "app"}`. Apostrophes inside a string (`"it's"`) are content, not syntax, and can stay as they are.

## Unquoted keys

JavaScript lets object keys go bare; JSON requires every key to be a double-quoted string. This shows up whenever an object literal is copied out of JavaScript source:

```
{name: "app", version: "1.0.0"}
```

Quote every key: `{"name": "app", "version": "1.0.0"}`.

## Comments

JSON has no comments: not `//`, not `/* */`, not `#`. If a config file needs them, use a format that allows them, such as JSONC (what VS Code parses), JSON5 or YAML, and strip the comments before the file goes to a strict JSON parser.

## Text before or after the JSON

A JSON document is exactly one value, with nothing before or after it. Three frequent offenders:

- **Anti-hijacking prefixes.** Some APIs start a response with a short prefix such as `)]}'`. Strip the prefix before parsing.
- **One object per line.** A log file with a JSON object on every line is NDJSON (newline-delimited JSON), not one JSON document. Parse each line separately.
- **Two values pasted together.** Split them, or wrap them in an array.

## Unescaped characters in strings

A literal line break cannot appear inside a JSON string; write it as `\n`. And a backslash starts an escape sequence, so a lone backslash followed by the wrong letter is an error. Windows paths hit this constantly:

```
"path": "C:\users\new"
```

Here `\n` reads as a newline and `\u` expects four hex digits, so the path breaks. Double every backslash:

```
"path": "C:\\users\\new"
```

## Numbers: leading zeros, NaN and Infinity

JSON numbers follow one strict format: no leading zeros (`01` is invalid), no hex (`0x1F`), no plus sign, and no `NaN` or `Infinity`, which exist in JavaScript but not in JSON. Where a value is genuinely missing or unlimited, use `null`.

## Duplicate keys

Not a syntax error, but a quiet data bug: the specification says keys should be unique, yet most parsers accept duplicates and silently keep the last value. The file parses, the data is wrong. Worth checking when valid JSON behaves oddly.

## Find the exact error fast

Hunting one bad character in a large file is slow. A validator that reports the exact line and column, and says in plain words what it expected, turns that hunt into one edit:

[Open the JSON validator](https://toolspace.tech/json-validator/)

Paste the file, jump to the reported position, fix it, check again. Once the file parses, the [JSON formatter](https://toolspace.tech/json-formatter/) pretty-prints it, which makes structural mistakes, like a closing brace one level too early, visible at a glance.
