You run JSON.parse on a one-line JSON file, and the error message ends with "at position 218". So you find a text editor that shows the column number, paste the line in, and move the cursor along until it says 219. Then you look at the characters around it and guess what went wrong.
There are quicker ways. Here are two.
Here's a line from a log with one comma missing:
const text =
'{"page":1,"per_page":3,"total":42,"data":[{"id":101,"name":"Ada Lovelace","email":"ada@example.com","roles":["admin","editor"],"active":true},{"id":102,"name":"Alan Turing","email":"alan@example.com","roles":["editor" "viewer"],"active":false},{"id":103,"name":"Grace Hopper","email":"grace@example.com","roles":["viewer"],"active":true}],"next":"/users?page=2"}';
try {
JSON.parse(text);
} catch (e) {
console.log(e.message);
}
// Expected ',' or ']' after array element in JSON at position 218 (line 1 column 219)
The quick way: slice around it
text.slice(200, 240);
// '"roles":["editor" "viewer"],"active":fal'
Position 218 is the quote that opens "viewer". There's no comma after "editor".
If you do this a lot: a small helper
function showError(text) {
try {
JSON.parse(text);
} catch (e) {
const m = /position (\d+)/.exec(e.message);
if (!m) throw e;
const pos = Number(m[1]);
const start = Math.max(0, pos - 30);
console.log(e.message);
console.log(text.slice(start, pos + 30));
console.log(' '.repeat(pos - start) + '^');
}
}
It prints the message, the text around the error, and a caret under the character.
Same problem in other languages
Python gives you a count too. The error ends with (char 218), and the exception has it as e.pos:
import json
def show_error(text):
try:
json.loads(text)
except json.JSONDecodeError as e:
start = max(0, e.pos - 30)
print(e)
print(text[start:e.pos + 30])
print(' ' * (e.pos - start) + '^')
Same text, same answer: Expecting ',' delimiter: line 1 column 219 (char 218). Slicing works the same way, text[200:240].
Browsers are less consistent. Chrome, Edge and Node give a position. Firefox gives a line and column but no position. Safari gives neither, only "Expected ']'".
Why does a trailing comma say "property name"
This one confused me at first:
JSON.parse('{"price":9.99,}');
// Expected double-quoted property name in JSON at position 14
Nothing is missing a name. After a comma, an object has to have another key. The parser hits } where it wanted a key, so that's what it complains about. The position points at the }, but the mistake is the comma just before it.
Python 3.13 and later names it directly: Illegal trailing comma before end of object. Older versions say Expecting property name enclosed in double quotes, which is the same story.
I've listed some of the common JSON errors we run into in different programming languages here: https://expresslint.com/fix-broken-json. It's my site. The same site also has tools to validate, visualise, compare and format JSON. It's free, requires no account, everything runs in your browser, and no JSON text is uploaded.
Hope it saves someone a few minutes.