Search code examples
google-apps-scriptgoogle-forms

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


Solution

  • If you're like me, you may have looked here to see how to store the JSON file inside the actual Apps Script project (e.g. alongside Code.gs) and then retrieve it in Code.gs.

    I did this:

    • Add an HTML file to your project.
    • Remove all the content of the HTML file and put in your JSON
    • In your Code.gs file, use something like the following:
    //Assume the file you added to your project is called 'my-json.html'
    
    const jsonString = HtmlService.createHtmlOutputFromFile("my-json.html").getContent();
    const jsonObject = JSON.parse(jsonString);