Menu

Tip: finding the error when JSON.parse fails on minified JSON

Reading 0:00
15s threshold

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":"[email protected]","roles":["admin","editor"],"active":true},{"id":102,"name":"Alan Turing","email":"[email protected]","roles":["editor" "viewer"],"active":false},{"id":103,"name":"Grace Hopper","email":"[email protected]","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…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.