How to Save a JSON file to Google Drive using Google Apps Script?
Asked Answered
O

2

6

I have a neural network script that requires training data from a JSON file, is it possible to save a JSON file to Google Drive using Google Apps Script?

Oarlock answered 12/10, 2018 at 10:26 Comment(0)
A
9

You can use the Advanced Drive Service. There are 3 options for creating files in Google Drive.

  • DriveApp - Built-in Apps Script service
  • Advanced Drive Service - Specific to Apps Script - Must be added.
  • Drive API - Must use the REST API option

Currently Apps Script has both the legacy and new code editor. Enabling the Advanced Drive Service is different in both editors.

Legacy Editor:

  • "Resources" menu
  • "Advanced Google Services"
  • Find "Drive API" in the list
  • Click the button to turn it ON

New Editor:

  • Click "Services" in the sidebar on the left
  • Find "Drive API" in the list, click it
  • Click the "Add" button

In the past, you needed to also enable the Drive API in the Google Cloud Platform. But now you have two choices.

  • Use the "default" Google Cloud project - You don't need to explicitly enable the Drive API in the Google Cloud Platform.
  • Create a new "standard" Google Cloud project and associate it with your Apps Script project. You will need to enable the Drive API in the Google Cloud Platform.

If the Apps Script project is going to be something used by the general public, then you should create a "standard" Google Cloud project, and enable the Google Drive API in the Google Cloud Platform.

Code:

function saveAsJSON() {
  var blob,file,fileSets,obj;
  
  obj = {//Object literal for testing purposes
    key:"value"
  }

/**
 * Creates a file in the users Google Drive
 */
  
  fileSets = {
    title: 'AAA_Test.json',
    mimeType: 'application/json'
  };
  
  blob = Utilities.newBlob(JSON.stringify(obj), "application/vnd.google-apps.script+json");
  file = Drive.Files.insert(fileSets, blob);
  Logger.log('ID: %s, File size (bytes): %s, type: %s', file.id, file.fileSize, file.mimeType);

}
Anticlerical answered 12/10, 2018 at 13:53 Comment(6)
How do you know it's application/vnd.google-apps.script+json? MimeType has no such definition: developers.google.com/apps-script/reference/base/mime-typeChurchwoman
Here is a link to official Google documentation with that mime type in it: Google Export Mime Types It's not easy to find.Anticlerical
Is there a meaningful difference between Drive.Files.insert(filesets, blob); as you have written and, say, DriveApp.getRootFolder().createFile('AAA_Test.json', JSON.stringify(obj), MimeType.PLAIN_TEXT);? Is there is a difference if/when one attempts to read the contents of the file? In my tests (and at first glance), the files appear identical in form and function, with one exception: when clicking on the file in GDrive, the "Details" pane will show "Type: Unknown File" for the former and "Type: Text" for the latter.Internist
Besides what I mentioned in my previous comment, the latter approach will provide a preview of the text content in the "Details" pane. However, another interesting difference between the two: when writing a file, my tests show that the former is 35% faster than the latter. But, only 10% faster when writing to a folder other than the root "My Drive".Internist
Thanks for posting your findings. The filesets mimetype should be "text/plain" or MimeType.PLAIN_TEXT For example: fileSets = {title: "AAA_Test.txt", mimeType: "text/plain"};Anticlerical
Another reason for using the Advanced Drive Service is that a more restricted scope can be used, so that the user doesn't need to give the code full access to their Google Drive. This is critical if you have a public app that shouldn't have full access to the users Drive. For example, being able to delete every file in their Drive. Also, to be approved for public use by Google, you'd need to use the more restricted Drive scope. That Drive scope/permission is https://www.googleapis.com/auth/drive.fileAnticlerical
S
1

Expanding @Brandon's comment about DriveApp to a separate answer:

Simplest solution is:

const obj = { key: "value" };
DriveApp.createFile("Test.json", JSON.stringify(obj));

Can write to an existing subfolder using:

const folder  = DriveApp.getFoldersByName("Your_Dir").next();
folder.createFile("Test.json", JSON.stringify(obj));

Or get the exact folder with DriveApp.getFolderById(the_share_id).


Setting the mime type with .createFile("Test.json", data, MimeType.PLAIN_TEXT) makes Drive show the file as "Type=Text" instead of "Unknown File"


.createFile() returns a File object with has methods like .getId().

Shushubert answered 20/8, 2023 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.