Good Day,
I'm trying to fetch data from my local JSON file located in the public folder of my react app. I know this is a frequently asked question in SO and I've already looked up topics related to my issue beforehand but this below is the nearest one.
Fetch local JSON file from public folder ReactJS
I'm using the code below to fetch my json file:
const getResponse = () => {
fetch('responseScript.json')
.then(response => { response.json() })
.then(data => { console.log(data) })
}
useEffect(() => {
getResponse();
})
The main issue is whenever I fetch data from my json file, I get undefined and an error:
responseScript is in the public folder. I've also checked devtools and in the network tab, I can see that the request status code is 200 OK.
I'm not sure if it adds to the issue, but I'm using typescript. I've also looked into using FileStructure and that doesn't seem to work for me also.
EDIT
My JSON structure looks like this:
[
{
"Category":"Some text here",
"Issue":"Some text here",
"Answer":"Some text here",
"Response":"Some text here"
},
{
"Category":"Some text here",
"Issue":"Some text here",
"Answer":"Some text here",
"Response":"Some text here"
},
{
"Category":"Some text here",
"Issue":"Some text here",
"Answer":"Some text here",
"Response":"Some text here"
}
]
The folder structure is
Any help is appreciated. Thanks!