Why does JSON.parse("string") fail

json

Solution

You are actually passing the bare word `string` in to the the function which of course is not valid JSON. To actually pass in the value `"string`" you need to be careful with your JavaScript.

Try this:

JSON.parse("\"string\"")

The extra pair of quotes must be escaped so they become part of the value you pass in to the function.

Problem

According to the JSON spec a string is a legitimate JSON value. So why does this happen?

Original source