Development 6 min read

By ToolByte Team · Published

The Seven JSON Errors You Will Hit Most, How to Read the Error Message, and How to Fix Each One

The Seven JSON Errors You Will Hit Most, How to Read the Error Message, and How to Fix Each One
json debugging json validator json formatter api

JSON is simple enough to learn in ten minutes and strict enough to break a deploy over a single comma. The specification has no comments, no trailing commas, no single quotes and no undefined, and every parser enforces all of it. That strictness is why JSON is reliable; it is also why "Unexpected token" is one of the most-searched error messages in software.

Here are the errors we see most often, what the parser is actually complaining about, and the fastest way to fix each.

Key takeaways

  • Nearly every JSON error is one of seven mistakes. Learn to recognise them and you can fix most by eye.
  • The error message tells you the position, not the cause. The real mistake is usually just before the position reported.
  • Validate before you deploy. A validator takes two seconds; a broken config file in production takes an afternoon.
  • Format (beautify) JSON before you debug it. Nobody can find a missing bracket in a 4,000-character single line.

How to read a JSON error message

Parsers report where they gave up, not where the mistake is. Take this:

{"name": "Widget", "price": 9.99,}

Node reports Unexpected token } in JSON at position 33. Position 33 is the closing brace. The brace is fine; the trailing comma before it is the problem. The parser saw the comma, expected another key, found } instead, and stopped there.

So when you get a position or line number: look at that spot, then look at what comes immediately before it.

Most parsers now give a line and column (Expected ',' or '}' after property value in JSON at position 14 (line 2 column 3)). The JSON Validator reports line-level hints and highlights the location, which is faster than counting characters.

The seven errors

1. Trailing comma

{
  "items": ["a", "b", "c",],
  "count": 3,
}

Both commas before a closing bracket are illegal. JavaScript allows them in object literals; JSON does not, and people writing JSON by hand after years of JavaScript do this constantly.

Fix: delete the comma after the last item. If a tool generated the file, the tool has a bug in its list-joining logic.

2. Single quotes

{'name': 'Widget'}

Strings and keys must use double quotes. Python dictionaries print with single quotes, and copying a print(dict) into a JSON file produces this error every time.

Fix: replace ' with ". In Python, use json.dumps(obj) instead of str(obj).

3. Unquoted keys

{name: "Widget", price: 9.99}

Valid JavaScript, invalid JSON. Every key is a string and needs quotes.

Fix: quote the keys. If you are pasting from a JS file, it was never JSON.

4. Comments

{
  // default timeout in seconds
  "timeout": 30
}

JSON has no comment syntax. Config files are the usual victim: someone adds a helpful note and the app refuses to start.

Fix: remove the comment, or put the note in a "_comment" key if the application ignores unknown keys. Some tools (VS Code's settings, for example) use "JSONC", JSON with comments, but a standard parser rejects it.

5. Unescaped characters in strings

{"path": "C:\Users\me", "quote": "She said "hello""}

Backslashes and double quotes inside a string must be escaped: \\ and \". Raw newlines inside a string are also illegal; use \n.

Fix: "C:\\Users\\me" and "She said \"hello\"". The String Escape tool does this for a block of text and is the safest way to embed user-supplied content or file paths.

6. The wrong literal

{"active": True, "owner": None, "score": NaN, "note": undefined}

JSON knows exactly three literals: true, false and null, all lowercase. True, None (Python), undefined and NaN (JavaScript) are all errors.

Fix: true, null, and for NaN or Infinity decide on a number or null, because JSON cannot represent them.

7. Invisible characters

The file looks perfect and still fails on line 1. The usual suspects:

  • A byte order mark (BOM) at the start, added by Windows Notepad and some exports. The parser sees an invisible character before {.
  • Smart quotes (“ ”) pasted from a document or chat app instead of straight quotes.
  • A non-breaking space where a regular space should be.

Fix: paste the content into the Whitespace Cleaner or re-save the file as UTF-8 without BOM. For smart quotes, find and replace “ and ” with ".

Debug in this order

  1. Beautify first. Paste the JSON into the JSON Beautifier. If it is valid, you get indented output and can read it. If it is invalid, the beautifier fails at the same spot the validator does, but the formatted part before the failure shows you the structure you are in.
  2. Validate. The JSON Validator gives you the position and a message. Look just before the position.
  3. Check the ends. If the error is at the very end of the file, you are missing a closing bracket or brace. If it is at the very start, it is a BOM or a stray character.
  4. Bisect large files. For a 10 MB payload, delete the second half and validate. If it passes (allowing for the now-missing closing bracket), the error is in the half you removed. Repeat.

Reading JSON you did not write

API responses and exports arrive minified, as one line. Three tools make them readable:

  • Beautify to indent it.
  • JSON to Table turns an array of objects into an HTML table, which is the fastest way to scan a list of records for the one with the odd value.
  • Minify again with the JSON Minifier before storing or sending, to remove the whitespace you added. Whitespace is typically 20–30% of a pretty-printed payload.

Preventing the errors

  • Never build JSON by string concatenation. Every language has a serialiser; use it.
  • Validate config files in CI with a one-line check (python -m json.tool config.json or jq empty config.json).
  • If a file must have comments, use YAML or TOML, or a JSONC-aware loader, and say so in the repository.
  • When a user or another system supplies text that will go inside a JSON string, escape it at the boundary, not three layers down.

Frequently asked questions

Why does my JSON work in JavaScript but fail in the API?

Because JavaScript object literal syntax is looser than JSON. Trailing commas, unquoted keys, single quotes and comments are all fine in a .js file and all illegal in JSON. Run it through a validator.

Is "Unexpected end of JSON input" the same thing?

It means the parser reached the end of the text while still inside an object, array or string. Usually a missing closing } or ], or a response that was cut off before it finished downloading.

Can JSON have duplicate keys?

The specification says keys should be unique but does not forbid duplicates, so validators accept them. Most parsers keep the last value. Treat duplicates as a bug.

Does the validator upload my data?

No. The JSON validator, beautifier and minifier run in your browser; the text does not leave your device.

Tools used in this guide

More from the blog