Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Google Sheets to JSON: How to Convert Your Data

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To convert Google Sheets to JSON, the most efficient method is using a Google Apps Script to create a custom web endpoint that serves your spreadsheet data as a JSON object. This allows you to transform a static table of information into a machine-readable format that can be used by websites, mobile apps, or other software integrations. By writing a small script within the sheet, you turn your spreadsheet into a lightweight API.

What You'll Need

  • A Google account with access to Google Sheets.
  • A spreadsheet containing the data you wish to convert.
  • The first row of your spreadsheet must contain headers (these will become the keys in your JSON objects).
  • Basic access to the Google Apps Script editor (built into Google Sheets).

Step-by-Step

  1. Prepare Your Data: Open your Google Sheet. Ensure that the first row contains clear, unique headers (e.g., "Name", "Email", "Phone"). Ensure there are no merged cells or empty rows between your header and your data, as this will break the mapping logic of the script.
  2. Open the Script Editor: In the top menu, go to Extensions > Apps Script. This will open a new browser tab with the Google Apps Script editor. Note that menu labels may differ slightly depending on your account region or version.
  3. Clear the Default Code: You will see a default function called myFunction(). Delete all existing code in the editor window to start with a blank slate.
  4. Insert the Conversion Script: Copy and paste the following logic into the editor. This script reads the active sheet, iterates through the rows, and maps each cell to its corresponding header:
    function doGet() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var data = sheet.getDataRange().getValues();
      var headers = data[0];
      var jsonArray = [];
    
      for (var i = 1; i < data.length; i++) {
        var obj = {};
        for (var j = 0; j < headers.length; j++) {
          obj[headers[j]] = data[i][j];
        }
        jsonArray.push(obj);
      }
    
      return ContentService.createTextOutput(JSON.stringify(jsonArray))
        .setMimeType(ContentService.MimeType.JSON);
    }
        
  5. Save the Project: Click the disk icon (Save) in the toolbar and give your project a name, such as "SheetsToJsonConverter".
  6. Deploy the Script: Click the Deploy button in the top right corner and select New deployment.
  7. Configure Deployment: In the deployment window, click the gear icon (Select type) and choose Web app. Under the configuration settings:
    • Set "Description" to something recognizable.
    • Set "Execute as" to Me.
    • Set "Who has access" to Anyone. This is necessary if you want external applications to fetch the JSON without complex OAuth authentication.
  8. Authorize the App: Click Deploy. A window will appear asking you to authorize access to your spreadsheet. Click "Authorize access", select your Google account, and if you see a warning saying "Google hasn't verified this app", click Advanced and then click "Go to SheetsToJsonConverter (unsafe)". This is normal for personal scripts.
  9. Get the URL: Once the deployment is complete, Google will provide a "Web App URL". Copy this URL. This link is now your JSON endpoint. When you visit this URL in a browser or call it via a script, it will return the current data from your sheet in JSON format.

Common Mistakes

One of the most frequent errors is leaving empty rows or columns within the data range. The getDataRange() method captures everything from cell A1 to the last cell containing data. If you have a random note written in cell Z100, the script will attempt to include all empty cells between your data and that note, resulting in a JSON object filled with null or empty string values.

Another common mistake is using duplicate headers. JSON keys must be unique within an object. If you have two columns both named "Date", the second "Date" column will overwrite the first one during the loop, and you will lose data in your final output. Always ensure your column headers are distinct.

Many users also forget to re-deploy the script after making changes to the code. If you modify the logic in the Apps Script editor and simply click save, the live Web App URL will still be running the old version of the code. To apply changes, you must go to Deploy > Manage deployments, edit the existing deployment, and change the version to "New version".

Tips

To make your JSON more readable during testing, you can use a browser extension like JSON Viewer. By default, the raw output from the Google Apps Script endpoint is a dense block of text. A viewer will format it with indentation and colors, making it easier to verify that your headers and values are mapped correctly.

If your spreadsheet is very large, you may encounter timeout issues. Google Apps Script has execution limits. For datasets exceeding a few thousand rows, consider filtering the data within the script or splitting the data across multiple sheets to keep the response time fast.

For better security, if you do not want the data to be public, change the "Who has access" setting to "Anyone with a Google account". However, this will require the requesting application to handle Google authentication headers, which adds complexity to your frontend code.

Troubleshooting

If you visit the Web App URL and see an error message or an empty array, first check your spreadsheet data. Ensure that there is at least one row of data beneath the header row. If the sheet is empty, the loop will not execute, and you will receive an empty bracket [].

If you receive a 401 or 403 Forbidden error, it is likely a permission issue. Go back to Deploy > Manage deployments and ensure that "Who has access" is set to "Anyone". If it is set to "Only myself", external requests will be blocked.

If the JSON output contains "undefined" or "null" values where you expect data, check for merged cells. Google Sheets treats merged cells as having a value only in the top-left cell of the merge; all other cells in the merged range are seen as empty. Unmerge all cells in your data range to ensure every value is captured.

If the script is not updating when you change the data in the sheet, remember that the script reads the current state of the sheet every time the URL is called. If the data isn't changing, ensure you have saved the spreadsheet (though Google Sheets autosaves) and that you are calling the correct deployment URL.

Frequently Asked Questions

Can I use this for a live website?

Yes, you can use the Web App URL as a data source for a website using the JavaScript fetch() API. This allows your website to update automatically whenever you edit the Google Sheet.

Is there a limit to how much data I can convert?

While there is no hard row limit for the conversion, Google Apps Script has a maximum execution time (usually 6 minutes) and a limit on the size of the HTTP response. Very large sheets may cause the request to time out.

Do I need to be a programmer to do this?

No. While this involves a script, you only need to copy and paste the provided code and follow the deployment steps. No actual coding knowledge is required to implement this specific solution.

Will the JSON update in real-time?

Yes. Because the doGet() function fetches the data from the sheet every time the URL is accessed, any changes you make to the cells will be reflected in the JSON output immediately upon the next refresh.

Can I convert the JSON back into a sheet?

This method is one-way (Sheet to JSON). To go from JSON back to a sheet, you would need a different script using doPost() or a third-party import tool.