If you are working with JavaScript probably, you’ll encounter syntaxerror: unexpected token o in json at position 1.
This error message might be confusing and frustrating when you are new to this and don’t know how to resolve it.
Luckily, this article discusses important details about this error and will show you how to fix the uncaught syntaxerror: unexpected token o in json at position 1.
What is “syntaxerror unexpected token o in json at position”?
The error message syntaxerror: unexpected token o in json at position 1 occurs when you are trying to use JSON.parse() method to parse, but the data is a JavaScript object, which means it is already parsed.
For example:
JSON.parse({website: 'itsourcecode'});
console.log(obj.website);
Output:
undefined:1
[object Object]
^
SyntaxError: Unexpected token o in JSON at position 1
In a nutshell, this error occurs when you try to parse the object which is already parsed.
When it is already a JavaScript object, it means no more parsing is necessary.
You can able use the value that is in your script. You just have to remove the JSON.parse() method.
For example:
const obj = {
website: 'Itsourcecode',
visits: 1000000,
offer: 'Free sourcecodes and tutorials',
};
console.log(obj.website);
console.log(obj.visits);
console.log(obj.offer);
Output:
Itsourcecode
1000000
Free sourcecodes and tutorials
What is JSON.parse()?
JSON.parse() is a method in JavaScript that is used to convert a JSON string into a JavaScript object.
It takes a JSON string as an argument and returns the corresponding JavaScript object.
The JSON.parse() method is commonly used to parse data received from a server in JSON format.
What is a JSON object?
A JSON object is a data structure that represents data in a human-readable format.
It is most likely the same as a JavaScript object, but the keys must be strings and the values must be valid JSON data types (string, number, object, array, boolean or null).
JSON objects are commonly used to transmit data between a server and a web application in the form of text.
Why does the “unexpected token o in json at position 1” error occurs?
This error can occur because of several reasons, that include the following:
❌ When you mistakenly used the JSON.parse method, but it is not necessary to use.
❌ When you are using JSON.parse and the data is a javascript object.
❌ When JSON data and string are parsed.
❌ When you forgot to place the full JSON strings in quotes.
How to fix “syntaxerror: unexpected token o in json at position 1”?
To fix the uncaught syntaxerror unexpected token o in json at position 1, avoid parsing an object that is already parsed because when it is already a JavaScript object, it means no more parsing is necessary.
Solution 1: Use JSON.stringify() method
When you are trying to convert a value to JSON, you can use JSON.stringify() method.
It will convert the data into a string before parsing it.
For example:
const json = JSON.stringify({website: 'itsourcecode'});
console.log(json);
console.log(typeof json);
Output:
{"website":"itsourcecode"}
string
If ever you need to use the JSON.parse() method, there’s a way to do that.
You just have to stringify the object first and then parse the JSON.
For example:
const parsed = JSON.parse(JSON.stringify({website: 'Itsourcecode'}));
console.log(parsed); }
console.log(typeof parsed);
Output:
{ website: 'Itsourcecode' }
object
Solution 2: Use userData
Frequently, if you use “userData” directly as an object, it can help to resolve this error.
For example:
var newData = userData.data.userList;
Solution 3: Use jQuerry
Alternatively, you can also use jquery, which will aid this error. Kindly refer to the example code below:
For example:
Var sampleval =JQuerry.parseJSON(JSON.stringify(data));
Solution 4: Use try/catch statement
Using the try/catch statement, you can handle a possible error in your code.
For example:
try {
const parsed = JSON.parse({});
} catch (error) {
console.log(error.message);
}
Conclusion
In conclusion, the error message syntaxerror: unexpected token o in json at position 1 occurs when you are trying to use JSON.parse() method to parse, but the data is a JavaScript object, which means it is already parsed.
This article already discussed what this error is all about and multiple ways to resolve this error.
By executing the solutions above, you can master this Syntaxerror with the help of this guide.
You could also check out other “SyntaxError” articles that may help you in the future if you encounter them.
- Syntaxerror: ‘break’ outside loop
- Syntaxerror unexpected token in json at position 0
- Syntaxerror: non-default argument follows default argument
We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊