Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load or Read a JSON from local in Google AppScript

I am looking to read a json file, namely config.json from a AppScript to generate a Google Form from the meta data in the config.json. I am not sure how to read the JSON file - could someone help me with it please? Some example code would be greatly appreciated.. Thanks

like image 741
g0c00l.g33k Avatar asked Dec 17 '22 19:12

g0c00l.g33k


1 Answers

Assuming that your file is in Google Drive, you can use the DriveApp service to read the file and parse it.

function getFileContent() {
  var fileName = "config.json.txt";
  var files = DriveApp.getFilesByName(fileName);
  if (files.hasNext()) {
    var file = files.next();
    var content = file.getBlob().getDataAsString();
    var json = JSON.parse(content);
    Logger.log(json);
  }
}

Make sure that Google Drive has not converted the text file into a native format else the script won't be able to parse it.

like image 172
Amit Agarwal Avatar answered Dec 28 '22 05:12

Amit Agarwal