close

[Solved] SyntaxError: Unexpected token < in JSON at position 0

I am facing the following error while parsing JSON data: SyntaxError: Unexpected token < in JSON at position 0 in ReactJS. In this Exerror article, We are going to learn about How to reproduce this error and we will discuss All Possible Solutions Lets Get Start with This Article.

How This SyntaxError: Unexpected token < in JSON at position 0 Occurs?

I am facing the following error while parsing JSON data.

SyntaxError: Unexpected token < in JSON at position 0

So here I am writing all the possible solutions that I have tried to resolve this error.

How To Solve SyntaxError: Unexpected token < in JSON at position 0 Error?

  1. How To Solve SyntaxError: Unexpected token < in JSON at position 0 Error?

    To Solve SyntaxError: Unexpected token < in JSON at position 0 Error You can also Use Try and catchto Prevent this error. while parsing JSON just use try if your JSON will be in the valid format then it will execute your code without any error. and If your JSON is invalid format then catch will handle your error. And now, your error will be solved. Thank you.

  2. SyntaxError: Unexpected token < in JSON at position 0

    To solve SyntaxError: Unexpected token < in JSON at position 0 Just make sure the value are trying to parse must be in Valid Json formate. This error usually occurs whenever you are parsing invalid JSON. Here is my example: var string = “Here is a string”; JSON.parse(string) While parsing the above string will give me SyntaxError so You need to Give valid JSON to the JSON.parse. Here I am passing valid JSON formate to JSON.parse. As I gave valid JSON My Code run successfully without any error. So You have to pass valid JSON and your error will be solved. Thank You.

Solution 1: Valid Json

To solve SyntaxError: Unexpected token < in JSON at position 0 Just make sure the value are trying to parse must be in Valid Json formate. This error usually occurs whenever you are parsing invalid JSON. Here is my example.

var string = "Here is a string";
JSON.parse(string)

While parsing the above string will give me SyntaxError so You need to Give valid JSON to the JSON.parse. Here I am passing valid JSON formate to JSON.parse. Just like this.

var myData= '{"foo" : "bar", "missedquotehere" : "value" }';
JSON.parse(myData)

As I gave valid JSON My Code run successfully without any error. So You have to pass valid JSON and your error will be solved. Thank You.

Solution 2: Use try catch

You can also Use Try and catchto Prevent this error. while parsing JSON just use try if your JSON will be in the valid format then it will execute your code without any error. and If your JSON is invalid format then catch will handle your error. Just like this.

try {
  const result = JSON.parse(apiData);
} catch (err) {
  console.log('Error: ', err.message);
}

And now, your error will be solved. Thank you.

Solution 3: If you are using fetch

If you are facing this error in fetch then you can us below code snippet.

  fetch('your_api')
    .then(response => {

      if (!response.ok) {
        throw new Error(`Error status: ${response.status}`);
      }

      return response.json();
    })
    .then(result => {
      console.log(result);
    })
    .catch(err => console.log(err));

Conclusion

It’s all About this error. I hope We Have solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Leave a Comment