I forgot a closing bracket in the middle of a JSON file. Why does the error point to a later line or to the very end?
Because the parser only knows a bracket is missing when it meets something that cannot appear in that place, which can be several lines later, or at the very end of the file when the outer brace is missing. The JSON formatter shows you that position and says plainly when the text ended too early; from there you work backwards to the last array or object that was never closed.
The parser only reads forward
Imagine reading a sentence with one word missing: you only realise something is missing when you reach a word that does not fit what came before. The parser does the same. It cannot know your intention; it reads character by character, and the error position is the first character where the text stopped being valid, not where the missing piece belongs.
Case one: an array never closed
{
"items": [1, 2,
"name": "x"
}
We forgot the ] after 2. The tool says: error at line 3, column 8. Why there? After the comma following 2 the parser is still inside the array, so it accepts "name" as a third element, a string. Then it meets the colon at column 8, which cannot appear inside an array, and stops. In a Chromium-based browser the message reads something like Expected ',' or ']' after array element. That ] in the message is the clue: an array is waiting to be closed.
Case two: the outer brace is missing
{
"items": [1, 2],
"name": "x"
Here the final } is missing. The tool says: Invalid JSON, the text ends before it is complete, at line 4, column 1. In other words, the file ended while the parser was still waiting for something. 'The text ends before it is complete' always means a bracket or a quote was opened and never closed.
Case three: the error on the very next line
{
"items": [1, 2
}
The ] is missing again, but the next line is the closing } straight away. The error is at line 3, column 1: the parser found the object's closing brace while still inside the array. Here the error sits close to the cause, but the cause is still on the line before.
How to work back to the cause
- Paste the file into the JSON formatter and validator and read the line, column and parser message.
- Press 'Go to the error position'. If the message mentions ], look for an open array; if it mentions }, look for an open object.
- Move upwards from the error position and find the last [ or { that was not closed before it.
- Add the missing bracket where it really belongs, which is not necessarily at the error position.
- Check that the tool says 'Valid JSON', then choose 'Format': the indentation in the output makes every level visible, so you can confirm each item landed in the right place.
Beware the quick fix: adding the bracket at the error position can make the text valid but with the wrong structure, such as the name key ending up inside the array. After fixing, look at the formatted output, not just the 'valid' message.
Common questions
Does the tool add the missing bracket automatically?
No. It repairs nothing, because it cannot know where you meant the array to end.
Why does the message wording differ between browsers?
The message comes from each browser's own parser. The line and column, which the tool shows in the same form everywhere, are what to rely on.
My file is very large; can I still check it?
Up to 5 MB. Open large files with 'Open a JSON file' rather than pasting; it is faster.
