react-nativepathreact-native-flatlistreadfilewritefile

How to read json file from directory and display in app


I have been trying to download and read JSON file from my application directory (which if obviously an array of objects) to map and display inside my app.

note-: downloading part is successful also I can read the file but it returns content of file as it is,I cannot map over its content. Is there a way I can do so..

RNFS.readFile(myfilePath, 'ascii')
.then(fileData => {
 console.log(fileData);
 })
.catch(err => {
console.log(err.message, err.code);
});

Solution

  • Use JSON.parse to convert json string to JavaScript value.

    RNFS.readFile(myfilePath, 'ascii')
      .then(fileData => {
        console.log(fileData);
        const data = JSON.parse(fileData)
      })
      .catch(err => {
        console.log(err.message, err.code);
      });