Did Google Sheets stop allowing json access?

17.2k Views Asked by At

I have an app that opens the json version of a spreadsheet that I've published to the web. I used the instructions on this website: https://www.freecodecamp.org/news/cjn-google-sheets-as-json-endpoint/

It's been working fine for a couple months, but today I realized that the url of my json file is no longer working since yesterday. It gives the message, "Sorry, unable to open the file at this time. Please check the address and try again." The regular link to view the spreadsheet as a webpage still works though.

Did Google drop support for this feature? Is there another way to get the data of a spreadsheet in json format through a URL? I started looking into the Google Developer API, but it was really confusing.

4

There are 4 best solutions below

7
doubleunary On BEST ANSWER

You are using the JSON Alt Type variant of the Google Data protocol. This protocol is dated and appears to no longer work reliably. The GData API Directory tells:

Google Spreadsheets Data API: GData version is still live. Replaced by the Google Sheets API v4.

Google Sheets API v4 is a modern RESTful interface that is typically used with a client library to handle authentication and batch processing of data requests. If you do not want to do a full-blown client implementation, David Kutcher offers the following v4 analog for the GData JSON Alt Type, using jQuery:

GData (old version, not recommended):

var url = 'https://spreadsheets.google.com/feeds/list/' +
           spreadsheet_id + '/' + tab_ordinal + '/public/values?alt=json';
($.getJSON(url, 'callback=?')).success(function(data) {
  // ...
};

V4 (new version, recommended):

var url = 'https://sheets.googleapis.com/v4/spreadsheets/' +
           spreadsheet_id + '/values/' + tab_name +
           '?alt=json&key=' + api_key;
($.getJSON(url, 'callback=?')).success(function(data) {
  // ...
};

...where:

  • spreadsheet_id is the long string of letters and numbers in the address of the spreadsheet — it is the bit between /d/ and /edit
  • tab_ordinal is number of the sheet — the first sheet that appears in the tab bar is sheet number 1, the second one is 2, and so on
  • tab_name is the name of the sheet, i.e., the name you see in the tab bar at the bottom of the window when you have the spreadsheet open for editing
  • api_key is the API key you get from from Google Cloud Platform console

Note that the JSON output format differs between the two versions.

With the GData pattern, the spreadsheet needs to be shared as File > Share > Publish to the web.

With the V4 pattern, the spreadsheet needs to be shared as File > Share > Share with others > anyone with the link can view.

0
Will Tan On

gdata is the older version of Sheets API and it's shut down. See Google's announcement here https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api

2
Mike Steelson On

Without jQuery ...

var url = 'https://docs.google.com/spreadsheets/d/'+id+'/gviz/tq?tqx=out:json&tq&gid='+gid;

with id of the spreadsheet and gid of the sheet https://codepen.io/mikesteelson/pen/wvevppe example :

var id = '______your_speadsheet_id________';
var gid = '0';
var url = 'https://docs.google.com/spreadsheets/d/'+id+'/gviz/tq?tqx=out:json&tq&gid='+gid;
fetch(url)
  .then(response => response.text())
  .then(data => document.getElementById("json").innerHTML=myItems(data.substring(47).slice(0, -2))  
  );
function myItems(jsonString){
  var json = JSON.parse(jsonString);
  var table = '<table><tr>'
  json.table.cols.forEach(colonne => table += '<th>' + colonne.label + '</th>')
  table += '</tr>'
  json.table.rows.forEach(ligne => {
    table += '<tr>'
    ligne.c.forEach(cellule => {
        try{var valeur = cellule.f ? cellule.f : cellule.v}
        catch(e){var valeur = ''}
        table += '<td>' + valeur + '</td>'
      }
    )
    table += '</tr>'
    }
  )
  table += '</table>'
  return table
}
0
A5H1Q On

As of March 2022:

If you dont want to create a key you can use this URL format:

https://docs.google.com/spreadsheets/d/{spreadsheetId}/gviz/tq

which downloads a json.txt file of the format

google.visualization.Query.setResponse({json});

From that you would have to slice out the json

-OR --

Just configure a key as per the Official docs.

  1. Go to Google Console and create a project (or use an existing one)
  2. Goto Credenetials page and create a API Key
  3. Include Sheets API from library
  4. And Voila!

You can now get json using URL Format:

https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{sheetName}?alt=json&key={theKey}

Edit: The Sheet should be public and Anyone with link can view