output a string as a JSON file with Google script

3,781 views
Skip to first unread message

Timothy Hanson

unread,
Mar 3, 2022, 8:53:31 PM3/3/22
to Google Apps Script Community
Looking for a script that will download a string as a JSON file

I have a string "strJSON" I create with multiple scripts. 

I output the string with showModalDialog

//Output to Html
  var template = HtmlService.createTemplateFromFile('copy');
  template.temp = strJSON;
  var htmlOutput = template.evaluate();
                  htmlOutput.setWidth(610)
                  htmlOutput.setHeight(750);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'AttributeRelevance');
I then copy the put and paste it into Notpad++  and save as a JSON file and this gives me a JSON file that validates and works as expected

Is there a way to output my string directly from Google as a JSON file? I have found scripts that will convert a Google sheet to a JSON but nothing that will save a string as a JSON file

Thanks in advance for any assistance with this

Tanaike

unread,
Mar 3, 2022, 10:18:18 PM3/3/22
to Google Apps Script Community
I believe your goal is as follows.

You want to download `strJSON` as a file when a dialog is opened.

In this case, how about the following sample script?

Google Apps Script side:

function openDialog() {
  var strJSON = JSON.stringify({"key1": "value1"}); // This is a sample value. Please replace this for your value.


  var template = HtmlService.createTemplateFromFile('copy');
  template.temp = `data:${MimeType.JSON};base64,${Utilities.base64Encode(strJSON)}`;

  var htmlOutput = template.evaluate();
  htmlOutput.setWidth(610)
  htmlOutput.setHeight(750);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'AttributeRelevance');
}

HTML & Javascript side:

<body></body>
<script>
  const filename = "sample.json"; // If you want to change the filename, please modify this.
  const a = document.createElement("a");
  document.body.appendChild(a);
  a.download = filename;
  a.href = <?= temp ?>;
  a.click();
</script>

I'm not sure about `copy` of your HTML file. So in this answer, I prepared a sample HTML and Javascript.

When the dialog is opened, "strJSON" is converted to the base64 data and it is downloaded as a file.

This sample is from https://stackoverflow.com/q/69375791/7108653

Timothy Hanson

unread,
Mar 4, 2022, 10:16:29 AM3/4/22
to Google Apps Script Community
Hello  Tanaike, 

That is great exactly what I hoped for!

I have a follow-up question if I may. 

Is it possible to a date stamp to the  "const filename = "Kumu.json";

something like:
const filename = "Kume.json" + date stamp;  

<!DOCTYPE html>
<html>
 <body></body>
  <script>
    const filename = "Kume.json"; // If you want to change the filename, please modify this.
    const a = document.createElement("a");
    document.body.appendChild(a);
    a.download = filename;
    a.href = <?= temp ?>;
    a.click();
  </script>
</html>

Thank you this is much appreciated!!
Tim

Timothy Hanson

unread,
Mar 4, 2022, 10:35:46 AM3/4/22
to Google Apps Script Community
I got it

<!DOCTYPE html>
<html>
 <body></body>
  <script>
    const formatYmd = date => date.toISOString().slice(0, 10);
          formatYmd(new Date()); 
    const filename = "Kumu" + "_" + formatYmd(new Date()) + ".json"; // If you want to change the filename, please modify this.
    const a = document.createElement("a");
    document.body.appendChild(a);
    a.download = filename;
    a.href = <?= temp ?>;
    a.click();
  </script>
</html>

Thank you again

Timothy Hanson

unread,
Mar 4, 2022, 10:56:07 AM3/4/22
to Google Apps Script Community
I have one more follow-up question 

The modal is empty, is it possible to not show the modal but still have the JSON download?

I tried commenting out
//SpreadsheetApp.getUi().showModalDialog(htmlOutput'AttributeRelevance');

but then the JSON did not download

Timothy Hanson

unread,
Mar 4, 2022, 11:10:08 AM3/4/22
to Google Apps Script Community
Or execute from a button click

Thank you

Timothy Hanson

unread,
Mar 4, 2022, 12:39:34 PM3/4/22
to Google Apps Script Community
Sorry about all these replies

I add 
google.script.host.close();

and this works

Thanks

Tanaike

unread,
Mar 4, 2022, 8:47:52 PM3/4/22
to Google Apps Script Community
Thank you for replying. I apologize for my late response. I'm glad your issue was resolved.
Reply all
Reply to author
Forward
0 new messages