Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Unexpected token ' in JSON at position 2

I've an encoded stringifyed JSON object stored in database, I decoded it and loaded it and tried to parse it into an object But I get

Uncaught SyntaxError: Unexpected token ' in JSON at position 2 at JSON.parse ()

Code:

var attr = new Object();
attr = JSON.parse(code[1].replace(/"/g, "'"));

Object decoded:

[{'inputs':0,'type':'variable'},{'inputD':0,'type':'variable'},{'inputI':0,'type':'variable'},{'paras':0,'type':'variable'},{'headers':0,'type':'variable'},{'menus':0,'type':'variable'},{'lists':0,'type':'variable'},{'divs':0,'type':'variable'},{'links':0,'type':'variable'},{'images':0,'type':'variable'},{'elemName':'{}','type':'object'},{'borders':[],'type':'array'},{'nested':[],'type':'array'},{'ribbons':[],'type':'array'},{'tooltips':[],'type':'array'},{'gradColors':'{}','type':'object'},{'events':'{}','type':'object'},{'sTarget':'{}','type':'object'},{'sMain':'{}','type':'object'},{'orignalStyle':'{}','type':'object'},{'objNewStyle':'{}','type':'object'},{'functions':'{}','type':'object'},{'reverse':'{}','type':'object'},{'reverseFunction':'{}','type':'object'},{'scDetails':'{}','type':'object'}]

like image 764
Ahmed Nezar Avatar asked Sep 07 '25 00:09

Ahmed Nezar


1 Answers

I have same error, @Philipp Zitzmann is correct. You must valid json string at https://jsonformatter.curiousconcept.com/

valid json string must have double quote.

JSON.parse({"u1":1000,"u2":1100})       // will be ok

no quote cause error

JSON.parse({u1:1000,u2:1100})    
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2

single quote cause error

JSON.parse({'u1':1000,'u2':1100})    
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2
like image 197
hoogw Avatar answered Sep 08 '25 22:09

hoogw