0
votes

I am completely baffled. I am getting an exception when trying to JSON.parse() the following string:

{"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"}

The exact error reported by the exception is:

JSON.parse: unexpected character at line 1 column 1 of the JSON data

The string syntax is correct according to JSONLint. I have also confirmed that the data type is string.

What could be causing the exception?

2
If the thing you're passing JSON.parse is really a string, and those are really its contents, you will not get that error. (Unless you've replaced JSON.parse with something nonstandard, which you probably haven't.) Update the question with a minimal reproducible example that we can actually see demonstrate the problem. - T.J. Crowder
Are you sure the input to JSON.parse is a string and not a JSON Object? - Karthik VU
Hey your input is object that is why your getting error - Anil Samal
@KarthikVU Please do not perpetuate the confusing and incorrect usage "JSON Object". In any case, he already said that it is a string. - user663031

2 Answers

0
votes

Maybe you forgot to quote the string?

console.log(JSON.parse('{"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"}'))

Omitting the quotes would yield an object literal:

console.log({"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"})
0
votes

You're probably assigning that value literally, wich is an object not a string, so:

var invalidJSON = {"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"};

var validJSON = '{"result":0,"extra":"2a9e10ecdeb8e65165c8635d51fd6b8f6fa7c10e951167a093ac1621e81dd7e8"}';