Save Google Sheet as JSON

The ExportSheetAsJSON method will convert the active sheet of a Google Spreadsheet to a JSON file and saves the file to your Google Drive. The header (first row) are used as item attributes in the JSON document.

/* From https://gist.github.com/IronistM/8be09ebd4c5a4a58c63b */

function exportSheetAsJSON() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var numCols = rows.getNumColumns();
  var values = rows.getValues();

  var output = '';
  output += '{"' + sheet.getName() + '" : {\n';
  var header = values[0];
  for (var i = 1; i < numRows; i++) {
    if (i > 1) output += ' , \n';
    var row = values[i];
    output += '"' + row[0] + '" : {';
    for (var a = 1; a < numCols; a++) {
      if (a > 1) output += ' , ';
      output += '"' + header[a] + '" : "' + row[a] + '"';
    }
    output += '}';
    //Logger.log(row);
  }
  output += '\n}}';
  Logger.log(output);

  DriveApp.createFile(sheet.getName() + '.json', output, MimeType.PLAIN_TEXT);
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻