React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 json json

React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0


Add two headers Content-Type and Accept to be equal to application/json.

handleGetJson(){  console.log("inside handleGetJson");  fetch(`./fr.json`, {      headers : {         'Content-Type': 'application/json',        'Accept': 'application/json'       }    })    .then((response) => response.json())    .then((messages) => {console.log("messages");});}


The solution that worked for me is that:-I moved my data.json file from src to public directory.Then used fetch API to fetch the file.

fetch('./data.json').then(response => {          console.log(response);          return response.json();        }).then(data => {          // Work with JSON data here          console.log(data);        }).catch(err => {          // Do something for an error here          console.log("Error Reading data " + err);        });

The problem was that after compiling react app the fetch request looks for the file at URL "http://localhost:3000/data.json" which is actually the public directory of my react app. But unfortunately while compiling react app data.json file is not moved from src to public directory. So we have to explicitly move data.json file from src to public directory.


This error can be received but be aware it can be a red herring to the real issue. In my case, there wasn't an issue with the JSON as the error states, but rather a 404 was occurring that it could not pull the JSON data to process in the 1st place thus resulting in this error.

The fix for this was that in order to use fetch on a .json file in a local project, the .json file must be accessible. This can be done by placing it in a folder such as the public folder in the root of the project. Once I moved the json file into that folder, the 404 turned into a 200, and the Unexpected token < in JSON at position 0 error was resolved.