Read Google Sheets as JSON
Read a public Google Sheet as JSON over HTTP, with the keyless gviz endpoint or the Sheets API v4 and an API key.
2 min readUpdated
A Google Sheet can serve as a small read-only data source for a script or a static site. This note shows two ways to read one over plain HTTP: the visualization (gviz) endpoint, which needs no key, and the Sheets API v4 with an API key.
Share the sheet
Both methods read the sheet anonymously, so it must be shared as Share > General access > Anyone with the link > Viewer. The sharing link has this form; the part between /d/ and /edit is the spreadsheet ID used below:
https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit?usp=sharingRead without an API key (gviz)
The gviz endpoint returns the whole first sheet:
https://docs.google.com/spreadsheets/d/{spreadsheetId}/gviz/tqAdd sheet to choose a sheet by name and tq to filter with a query in the Google Visualization API query language. Columns are referred to by letter, and the query must be URL-encoded. This returns the rows of {sheetName} where column A equals 27938:
https://docs.google.com/spreadsheets/d/{spreadsheetId}/gviz/tq?sheet={sheetName}&tq=select+*+where+A=27938The response is JSON wrapped in a JavaScript call, so strip everything before the first { and after the last } before parsing it:
/*O_o*/google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","table":{"cols":[...],"rows":[...]}});Rows are in table.rows, each with a c array of cells whose value is in v. If CSV is easier to handle, add tqx=out:csv to get the result as plain CSV instead.
Read with the Sheets API v4
The official API returns clean JSON but needs an API key:
- In the Google Cloud console, create a project or select an existing one.
- Under APIs & Services > Library, enable the Google Sheets API.
- Under APIs & Services > Credentials, create an API key. Restrict it to the Sheets API.
Request the values of a sheet; {sheetName} can also be any A1 range such as Sheet1!A1:D10:
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{sheetName}?alt=json&key={theKey}The response has a values array with one array per row. JSON is the default format, so alt=json is optional.