Ad

Weird Error On Trying To Parse An Stringed Array With JSON

- 1 answer

I'm storing some information in Base64 encoded python lists, then decode them in javascript. However it does not parse my "list" as an array (the syntax is the same) because it gives me this error:

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

So it turns out, myString = "['foo']" returns this error, but myString = '["foo"]' works just fine. (In firefox at least)

Why does this happen? It makes zero sense, the quotation marks are not the same, so why does it throw an error?

Python always returns the string wrapped in "" and the actual contents of the list wrapped in '' so there is no way to change that.

Ad

Answer

JSON uses " to wrap strings, not ', thus 'foo' is not valid JSON string.

Ad
source: stackoverflow.com
Ad