When using JSON.parse
, it typically parses a stringified JSON
. However, if a variable is already in the form of JSON
and you attempt to parse it with JSON.parse
, an error is thrown:
> a
[]
> a = JSON.parse(a)
SyntaxError: Unexpected end of input
at Object.parse (native)
at repl:1:10
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
One might wonder why JSON.parse
doesn't simply verify that the argument is already a JSON object
and do nothing in that case instead of throwing an error.