SyntaxError: Unexpected token 'in JSON at position 1 [JAVASCRIPT]

0

Friends, I need your help. I have problems on the JavaScript side to convert a string to JSON, the error message says: SyntaxError: Unexpected token 'in JSON at position 1 and on the screen I show the string I receive. Thanks for your time :)

    
asked by Antonio Labra 28.08.2018 в 05:22
source

2 answers

3

The problem is that the JSON format has double quotes

  

"these"

and the string is with single quotes

  

'these others'

so that JSON.parse can not convert the string into an object. It seems that you are building this json in a string concatenated with variables.

If that is the case the real solution is to put double quotes instead of single quotes

  

{"name": "DSADA", "last name": "OIUOIU"}

Working example:

let string = '{"nombre":"Antonio","telefono":"5544632354"}';
console.log(JSON.parse(string))
console.log("mi nombre es", JSON.parse(string).nombre)
    
answered by 04.12.2018 / 09:07
source
0

THIS IS THE ANSWER:

  

var obj = data.replace (/ '/ g, "\" "); // Replace" with "

obj = JSON.parse(obj);
    
answered by 28.08.2018 в 06:08