Every developer knows this moment. You paste a JSON response into your editor, or you load a config file, and the only feedback you get is one short line:
Unexpected token t in JSON at position 1234 Or worse, the newer message that tells you almost nothing:
Unexpected token 't', ..."t": "value"... is not valid JSON The data is broken somewhere. The error knows where. Your job is to make it tell you, in a way a human can act on. This post covers the handful of mistakes that cause almost every invalid JSON error, and a small trick to turn that position number into the exact character that failed.
First, what "position" actually means
The position in a JSON.parse error is a character count, starting at zero, counted from the very first character of the string. It is not a line number and it is not a column. Position 1234 means the parser read 1234 characters happily, then hit something it could not accept.
That sounds useless until you print the characters around it. Here is the whole trick, in plain JavaScript:
function showJsonError(text, err) { const match = /position (\d+)/.exec(err.message); if (!match) { console.log("No position in this error message."); console.log(err.message); return; } const pos = Number(match[1]); const start = Math.max(0, pos - 40); const end = Math.min(text.length, pos + 40); console.log(text.slice(start, end)); console.log(" ".repeat(pos - start) + "^ right here"); } Feed it the raw string and the caught error, and you get the neighbourhood of the failure with a small arrow under the character that stopped the parser. Nine times out of ten, you will see the problem the moment that window prints.
The five mistakes that cause almost everything
1. The trailing comma. JSON does not allow a comma after the last item in an object or array. JavaScript does, which is why this one slips in from hand-written files all the time:
{ "name": "report", "pages": 12, } That final comma is the single most common cause of invalid JSON in config files and fixtures.
2. Single quotes. JSON strings must use double quotes. 'name': 'report' is fine JavaScript and invalid JSON. This one usually arrives from Python, where a dict printed with single quotes looks like JSON but is not.
3. Unquoted keys. Every key in JSON is a string, so every key needs its double quotes. {name: "report"} fails at the very first key.
4. Comments. JSON has no comments. A // note to self inside a .json file breaks the parse at the slash. If you need comments in a config file, you want JSONC or JSON5, and a parser that accepts them on purpose, not by accident.
5. Values JSON does not have. NaN, Infinity, undefined and functions are not JSON values. JSON.stringify quietly turns some of them into null, which means the file you saved is valid but no longer says what you meant. That is the worst kind of bug, because nothing ever throws.
A quick way to check before you dig
When the input is long, do not read it top to bottom. Work in this order:
- Run it through the position trick above and look at the window it prints.
- If the window looks clean, the problem is often just before it. Parsers report where they gave up, and a missing quote or bracket earlier can make a healthy character look guilty.
- Check the end of the string. A truncated response, cut off mid-value by a network hiccup or a file copy that stopped early, fails at the last character and the start of the file will look perfect.
- If the JSON came from another language, look for single quotes and Python or Java values like
True,NoneandNaNwearing JSON clothes.
When you would rather not write the helper
I keep the little function above in a scratch file, but on a machine that is not mine, or with a response I should not paste into a random tab, I use a browser-based formatter that shows the error position and highlights the spot in the text. Full disclosure: I build ToolPeko, a site of free tools that run in the browser, and its JSON formatter is the one I reach for. The formatting happens on your own device, which matters when the JSON holds anything private, like tokens from a real API response.
That last point is worth keeping even if you never use the tool. A JSON string from production often carries emails, session tokens and internal URLs. Pasting it into the first online formatter you find means sending it to a stranger's server. A formatter that runs in the page, or a local helper like the one above, keeps the data where it belongs.
The short version
- The position number is a character offset. Print the text around it and the bug usually shows itself.
- Trailing commas, single quotes, unquoted keys, comments and non-JSON values cause almost every failure.
- A parser points at where it gave up, which can be a few characters after the real mistake.
- Keep private JSON off other people's servers. Format it locally or in the browser.
Once you have seen the arrow trick work on one ugly production response, you will never squint at a raw position number again.


