Fix the unexpected token JSON at O error in JavaScript
In this tutorial, we are going to learn about how to Fix the unexpected token JSON at O error in JavaScript.
When we try to parse a non json using JSON.parse()
method sometimes we get the following error in our console.
message": "Uncaught SyntaxError: Unexpected token 'o',
\"[object Obj\"... is not valid JSON",
To fix this error, first we need to stringify the json using JSON.stringfy()
method then parse it using JSON.parse()
method.
Here is an example:
const users = [
{
name: "Adam",
price: 10,
},
{
name: "JOY",
age: 12,
},
];
const result = JSON.stringify(users)
console.log(JSON.parse(result));