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 :)
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)
THIS IS THE ANSWER:
var obj = data.replace (/ '/ g, "\" "); // Replace" with "
obj = JSON.parse(obj);