Write and retrieve JSON file to Google Drive using Google Apps Script

Ari

I am writing a program that takes in the data from a large Google Sheet, treats and manipulates it and makes connection to other sheets in the same file. In order to improve my runtime and make a backup database.

I would like to store the data to a JSON file somewhere that I can easily use in Google Apps script again (I believe Google Drive is the best free option). How do I accomplish this?

I appreciate if you can show the exact codes that saves the following file to Google Drive and then reads it back into the script.

let listA = [{id: 34, performance: 200, supervisor: 'A', name: 'Aethelwich'},
    {id: 35, performance: 300, supervisor: 'B', name: 'Aethelram'},
    {id: 36, performance: 400, supervisor: 'A', name: 'Aethelham'},
    {id: 41, performance: 500, supervisor: 'A', name: 'Aethelfred'}];

let jsonObject = JSON.stringify(listA);

// 1. code to save this jsonObject to Google Drive
// 2. code to retrieve the same saved JSON file back from google drive

Tanaike

In order to achieve your goal, how about the following sample script?

Sample script:

let listA = [{ id: 34, performance: 200, supervisor: 'A', name: 'Aethelwich' },
{ id: 35, performance: 300, supervisor: 'B', name: 'Aethelram' },
{ id: 36, performance: 400, supervisor: 'A', name: 'Aethelham' },
{ id: 41, performance: 500, supervisor: 'A', name: 'Aethelfred' }];

let jsonObject = JSON.stringify(listA);

// Save the data as a file.
const file = DriveApp.createFile("sample.txt", jsonObject); // Or, if you want to put the file to the specific folder, please use DriveApp.getFolderById("folderID").createFile("sample.txt", jsonObject)
const fileId = file.getId();
console.log(fileId)

// Retrieve the data from the file.
const text = DriveApp.getFileById(fileId).getBlob().getDataAsString(); // please set the file ID of the saved file.
const array = JSON.parse(text);
console.log(array)

References:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Upload file to my google drive with Google Apps Script (NO FORM IN GOOGLE)

using Google Apps Script: how to convert/export a Drive file?

Using Google App Script Unzip a file from Google Drive

Google Team Drive Move file between team drive folders using Apps Script

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

Google Apps Script - Using URL of File in Drive in =IMAGE() Spreadsheet Formula

render HTML file in Drive in a Google Apps Script web app

How to access a password protected PDF file on Google Drive using Apps Script

Google Apps Script moving file to Team Drive folder

Not deleting file from Google Drive using Google Script + Google SpreadSheet

How to retrieve JSON specific content from Google Drive with Google Apps Script?

Copying Gmail attachments to Google Drive using Apps Script

How to log Google Drive File Names to Google Sheets using Google Apps Script

Check zip file contents in Google Drive using Apps Script not unzipping it?

Create new file in a folder with Apps Script using Google Advanced Drive service

Save ip camera video to google drive using apps script UrlFetchApp

How to Attach Google Drive File with sendMail Function in Apps Script?

Google Apps Script - get URL of File in Drive with File Name

Google Drive Notification In Google Apps Script

Upload files to Google Drive using Google Apps Script

Retrieve data from Firebase using Google Apps Script

Drive.Files.get(fileid).LastModifiyingUser.emailaddress can't find the file in the shared drive using Google Apps Script

Failing to get a file in Google Apps Script using the Drive API

How do I use Google Apps Script to extract data from a JSON file on Google Drive and push the array into a table in Google Sheets?

Google apps script: Parsing JSON and write them to google spreadsheets

how to upload audio file to google drive with url with google apps script

Interact with svg file saved in Google Drive with Google Apps Script

Setting new Drive Labels to a specific file using Google Apps Script

How to retrieve a spreadsheet apps script id using google api