0

I am trying to load a static json file locally in my expo managed app as follow.

useEffect(()=>{

getData()},['']);

function getData(){

fetch('../Audio/AudiosObj.json',{'Content-Type':'application/json','Accept': application/json'}).then((data)=>(console.log(data)).catch((err)=>(console.log(err)) }

After running the app I got an error with message "Network request failed". Please what could be the problem?

1 Answer 1

2

I think you don't need to use fetch to get the json data. You can simply import the json directly using import statement.

import AudiosObjJSON from '../Audio/AudiosObj.json';

Otherwise if you still want to use fetch, you have provide the headers properly like this:

fetch('../Audio/AudiosObj.json',{
   headers: { 
     'Content-Type': 'application/json',
     'Accept': 'application/json'
   }
})
.then(...)
.catch(...);

But I will still recommend using import statement as they work fine & much more readable than doing a fetch call.

Also, the fetch implementation might not work properly after you compile the app & include all the assets as the relative path might not work.

But when using import statement, the file will be part of the bundle itself & should not cause any issue after it is compiled.

Sign up to request clarification or add additional context in comments.

3 Comments

I previously work with the method you suggested but I have some promise issues then I just switched to true the fetch api.
Were you getting any error when using the import statement? Can you share what the error message was?
I just use the import method and it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.