Did Google Sheets stop allowing json access?
Asked Answered
C

5

16

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.

Confectionery answered 18/8, 2021 at 5:23 Comment(2)
You can get the answer for the similar question. https://mcmap.net/q/747532/-kotlin-read-and-display-data-from-google-sheetsRalph
Although it's still possible to get the JSON, I recommend switching to the CSV API. See How to export a csv from Google Sheet API? to build the URL, then Papaparse to parse the CSV.Baggy
E
27

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.

Esmeraldaesmerelda answered 18/8, 2021 at 8:12 Comment(7)
Nice, so I'm able to access the data using the new URL format. But the app I'm using just takes a URL and the "coordinates" of a piece of data in a JSON file and retrieves that data. As in, something like ".feed.entry[17].gs$cell.$t". But this new format doesn't label each section of data. Is there something I can add to the URL that makes it a "classic" JSON file like what I had before?Confectionery
No, I do not think so. It is a different API with a different interface and a different result format. You will have to talk to the people who make the "app" you mention.Esmeraldaesmerelda
Oh ok, thank you. I'll probably just use regex instead of their inbuilt JSON function.Confectionery
It would appear that the v4 API requires an API key to be sent? Or is there a way that you can register your API so a key is not needed to access some spreadsheet data?Sharkey
hii @Esmeraldaesmerelda i tried his but it gives me error with code 403 with this json format , how do i get the access of the sheet. The jsonFormat is "error": { "code": 403, "message": "The caller does not have permission", "status": "PERMISSION_DENIED" } }Ralph
@JAY_Panchal check your sharing settings. Edited the answer.Esmeraldaesmerelda
yes i was not sharing it to public it was private thanks for the help @doubleunary🙌Ralph
I
18

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

Inutility answered 3/3, 2022 at 9:4 Comment(0)
C
4

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
}
Catercornered answered 27/8, 2021 at 4:59 Comment(2)
Nice, this seems to mostly work, but do you know if there's a way to get the data without "/*O_o*/ google.visualization.Query.setResponse(" preceding the data?Confectionery
Take this part of the response data.substring(47).slice(0, -2) or data.substring(117).slice(0, -3) and parse it as a json.Catercornered
S
2

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

Strigil answered 27/8, 2021 at 3:52 Comment(0)
O
-1

I belive https://sheet2api.com can do this.

Example JS:

import Sheet2API from 'sheet2api-js';

// To get your own URL you need to go to the site and paste in your Google Sheet URL to generate one.
const url = 'https://sheet2api.com/v1/FgI6zV8qT121/characters/';
const options = {};
Sheet2API.read(url, options).then(function(result){
    console.log(result);
}, function(error){
    console.log(error);
});
Originally answered 26/4, 2024 at 12:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.