How to Fix Invalid JSON: The 7 Errors That Break Every Parser

June 20, 2026 · 8 min read
Try the toolJSON Formatter →

JSON parsing sounds trivial until a single stray character breaks your build five minutes before a deploy. JSON is intentionally strict — stricter than JavaScript, which is exactly why developers coming from JS keep getting caught out. The good news is that almost every "invalid JSON" error is one of seven common mistakes. Learn to recognize them and most broken JSON is one character away from valid.

1. Trailing commas (the number one culprit)

This is the single most common JSON error. JavaScript lets you leave a comma after the last item in an object or array; JSON, per its RFC 8259 specification, does not. So {"name": "Alice", "age": 30,} is invalid because of that comma after 30, and ["a", "b",] is invalid for the same reason. The fix is simply to remove the comma after the final property or array element. This one bites everyone, from juniors hand-editing a config file to seniors pasting a snippet out of JavaScript.

2. Single quotes instead of double quotes

JSON requires double quotes around both keys and string values. Single quotes are valid in JavaScript and Python, but not in JSON. So {'name': 'Alice'} is invalid; it must be {"name": "Alice"}. This usually happens when your payload came from a JavaScript console.log or Python's str() output. A find-and-replace of single to double quotes often works, but watch out for apostrophes inside strings — "it's" would break — so a parser-aware fix is safer.

3. Unquoted keys

In JavaScript you can write object keys without quotes: {name: "Alice"}. JSON does not allow this — every key must be a double-quoted string. So {name: "Alice"} must become {"name": "Alice"}. This is another symptom of copying a JavaScript object literal directly into a JSON context.

4. Missing or mismatched brackets

Every opening brace needs a closing brace, and every opening square bracket needs its pair. When they do not match, you get the dreaded "Unexpected end of JSON input." These are hard to spot by eye in a large file, which is where a formatter that pretty-prints and highlights bracket matching earns its keep — mismatched brackets jump out the moment the structure is indented.

5. Comments

JSON has no comments. Not // line comments, not block comments. If you added a comment to explain a field, the parser rejects the whole file. VS Code's settings use a variant called JSONC that allows comments, and there is a superset called JSON5 that allows comments, single quotes, and trailing commas — but standard parsers like JSON.parse, Python's json module, and most server libraries only accept strict JSON. For config files that need comments, YAML or JSONC are better choices than bending JSON.

6. JavaScript values that are not valid JSON

Three values exist in JavaScript but not in JSON: undefined, NaN, and Infinity. If your data source produced one of these — common when serializing objects that contain a failed numeric conversion — the result is invalid JSON. The fix is to replace them with null (the JSON equivalent of "no value") or a sensible default. The bug is usually upstream, at the point where the data was generated, not in the JSON itself.

7. Unescaped characters in strings

Certain characters must be escaped with a backslash inside a JSON string. A raw newline, a tab, or a literal backslash will cause a parse failure. So a Windows path like "C:\Users\Alice" is invalid — the backslashes must be doubled to "C:\\Users\\Alice" — and a real line break inside a string must be written as \n. These are invisible in many editors, which makes them frustrating to hunt down.

The fastest debugging workflow

The worst JSON error message is just "invalid json" with no location. A good validator does three things: tells you what is wrong, tells you where (line and column), and shows the surrounding context. The fastest workflow is to paste the file into a validator, read the line it points to — and then check the line above it, because the parser often only realizes something is wrong when it hits the next token, so a missing comma on line 4 shows up as an error on line 5. If the error points to position 0, check for a byte-order mark or stray text before the opening bracket.

The TextCaret JSON Formatter uses the browser's native JSON engine to validate and pretty-print your JSON, so you get the same precise error reporting as your dev tools — and because it runs locally, JSON containing keys or personal data never leaves your machine.

Preventing invalid JSON

Three habits prevent most JSON pain. Never hand-edit large JSON files without a tool that respects the grammar. Pretty-print on save, because structural errors are obvious in indented JSON and hidden in a compact single line. And validate before committing — a simple check in your CI pipeline catches syntax errors before they reach any environment. Most broken JSON is one character from valid; the trick is catching that character early.

Try the toolJSON Formatter →