. Advertisement .
..3..
. Advertisement .
..4..
The “Unexpected token < in json at position 0” error must have annoyed numerous Javascript users. No worry; keep reading this article for extensive analysis and solutions!
What Is JSON?
JSON stands for JavaScript Object Notation. It is among the most common data formats that permit data transmissions. This format is compact and incorporates name-value pairs. These values can be arrays, strings, or any type of data that suits serialization.
Back then, programmers often used XML for data interchange. Nevertheless, ever since JSON launched, everyone has turned to it as an XML replacement.
In most cases, JSON files will end with the “.json” extension. We provide a simple example below to help you understand the concept better.
{
"name": "animals",
"animals": [
"Dog",
"Cat",
"Mouse",
"Pig",
"Bear"
],
"hobbies": {
"football": false,
"reading": true
}
}
JSON
JSON formats bear a lot of resemblance to JavaScript objects. Still, keep in mind that such sentiments are not always true. Also, do not forget that double quotes must wrap up all your property names.
// not valid JSON
{
x: 4
}
// valid JSON
{
"x": 4
}
Why Did The Error “Unexpected Token < In Json at Position 0” Occur?
Such errors often stem from the HTML (instead of JSON) that your server sends back (often starting with <html> or <DOCTYPE html>). A valid JSON is not supposed to start with < characters. Hence, your JSON parser immediately realizes that the data is not a valid JSON. The result is an error message that you have seen above.
So how can we rectify this mistake? First, identify why you are receiving HTML (or anything other than your expected JSON). For this task, log the data where you attempt a console parse. Check out the next section for more details.
How to Fix This Error?
First, try to log out of the system. Now you may adopt the res.text() – rather than res.json() – to obtain that text string. Alter the code and inspect the console to locate the core issue:
fetch('/users')
// .then(res => res.json()) // comment this out for now
.then(res => res.text()) // convert to plain text
.then(text => console.log(text)) // then log it out
Remember that res. text() and res. json() are asynchronous; hence, it is impossible to log the return value instantly. As a result, your “console.log” should be in an independent “separate. then” block.
The issue becomes much simpler for other programmers who use “JSON.parse” directly. After all, that is a senior synchronous call, and it is feasible to replace that call with “console.log” to investigate the problem.
// JSON.parse(someString) // comment this out temporarily
console.log(someString) // log out out, and see what's wrong
Conclusion
This article has delivered some simple solutions to tackle the error message: “Unexpected token < in json at position 0″. In certain cases, upon activating the firebase, another message might pop up: “Unexpected reserved word ‘await“. Check out this article for more info on such syntax errors!
Leave a comment