Uncaught SyntaxError: Unexpected token with JSON.parse

coiso picture coiso · Jan 21, 2013 · Viewed 934.6k times · Source

what causes this error on the third line?

Open console to view error

Answer

SLaks picture SLaks · Jan 21, 2013

products is an object. (creating from an object literal)

JSON.parse() is used to convert a string containing JSON notation into a Javascript object.

Your code turns the object into a string (by calling .toString()) in order to try to parse it as JSON text.
The default .toString() returns "[object Object]", which is not valid JSON; hence the error.