How do you find which line of a JSON file has the syntax error?

Paste the file into a validator. In Chrome and Edge the parser reports a character position you can convert to a line and column by counting newlines; Firefox's message already names the line and column.

What the error tells you

When JSON.parse fails, Chrome and Edge say something like Unexpected token } in JSON at position 1482. That position is a character offset into the whole text, counting from 0. Firefox skips the offset and writes the line and column straight into the message.

Position to line and column

function positionToLineCol(text, pos) {
  const before = text.slice(0, pos);
  const lines = before.split('\n');
  return { line: lines.length, col: lines[lines.length - 1].length + 1 };
}

The usual suspects at that spot: a trailing comma after the last item, single quotes instead of double quotes, an unquoted key, or a comment, which JSON does not allow.

Faster: let the page do it

The formatter below runs this conversion for you and shows the line and column next to the parser's message. It parses in your browser, so nothing you paste leaves the page.

Do the math with your own numbers:

JSON Formatter and Validator →

Free, runs in your browser. Nothing you type is sent anywhere.

Advertisement