Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this string unparseable?

JSON.parse('["foo", "bar\\"]'); //Uncaught SyntaxError: Unexpected end of JSON input

When I look at the above code everything seems grammatically correct. It's a JSON string that I assumed would be able to be converted back to an array that contains the string "foo", and the string "bar\" since the first backslash escapes the second backslash.

So why is there an unexpected end of input? I'm assuming it has something to do with the backslashes, but I can't figure it out.

like image 253
Gwater17 Avatar asked Sep 27 '16 03:09

Gwater17


1 Answers

It seems like your code should be:

JSON.parse('["foo", "bar\\\\"]');

Your Json object is indeed ["foo", "bar\\"] but if you want it to be represented in a JavaScript code you need to escape again the \ characters, thus having four \ characters.

Regards

like image 111
SorMun Avatar answered Oct 21 '22 01:10

SorMun