Hello Guys, How are you all? Hope You all Are Fine. Today I am parsing data but I am facing Following Error Uncaught SyntaxError: Unexpected token o in JSON at position 1 in Javascript. So Here I am Explain to you all the possible solutions here.
Without wasting your time, Let’s start This Article to Solve This Error.
How Uncaught SyntaxError: Unexpected token o in JSON at position 1 Error Occurs ?
I am parsing data But I am facing the Following error.
Uncaught SyntaxError: Unexpected token o in JSON at position 1
Here is my code.
var userData = usersPersonalData;
var newData = JSON.parse(userData).data.userList;
How To Solve Uncaught SyntaxError: Unexpected token o in JSON at position 1 Error ?
- How To Solve Uncaught SyntaxError: Unexpected token o in JSON at position 1 Error?
To Solve Uncaught SyntaxError: Unexpected token o in JSON at position 1 Error Here are the first parameters of the function JSON.parse should be a String, and your data is a JavaScript object, so it will convert to a String [object object], you should use JSON.stringify before passing the data. Here is Some Example.
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
To Solve Uncaught SyntaxError: Unexpected token o in JSON at position 1 Error Here are the first parameters of the function JSON.parse should be a String, and your data is a JavaScript object, so it will convert to a String [object object], you should use JSON.stringify before passing the data. Here is Some Example.
Solution 1
Let me Explain Why you are facing this error Here are the first parameters of the function JSON.parse
should be a String, and your data is a JavaScript object, so it will convert to a String [object object]
, you should use JSON.stringify
before passing the data. Here is Some Example.
new Object().toString()
// "[object Object]"
JSON.parse(new Object())
// Error: Uncaught SyntaxError: Unexpected token o in JSON at position 1
JSON.parse("[object Object]")
// Error: Uncaught SyntaxError: Unexpected token o in JSON at position 1
JSON.parse(JSON.stringify(userData))
// This Will Work
So Here You should Use
JSON.parse(JSON.stringify(userData))
Solution 2
Here JSON.parse()
converts the input into a string. The toString()
method of JavaScript objects by default returns [object Object]
, resulting in the observed behavior. So that you just need to use this.
var newData = userData.data.userList;
Summery
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also Read