Search code examples
javascriptjsonsyntax-errorspecial-characterstoken

Uncaught SyntaxError: Unexpected token i


a = JSON.parse('["<input class="image" type="file" name="%simg_%d"/>"]');

gives the above error. Can someone explain why? Is %s the start of some kind of special character string in json/javascript?


Solution

  • You don't escape the quotes so you keep breaking in and out of the string which makes for invalid JSON

    JSON.parse('["<input class=\\"image\\" type=\\"file\\" name=\\"%simg_%d\\"/>"]');