Skip to content

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=sharing

Read without an API key (gviz)

The gviz endpoint returns the whole first sheet:

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

Add 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=27938

The 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:

  1. In the Google Cloud console, create a project or select an existing one.
  2. Under APIs & Services > Library, enable the Google Sheets API.
  3. 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.

References

Related notes

2 min read

gRPC

What gRPC is, how it differs from JSON over REST, and its four kinds of RPC with a Protocol Buffers service example.

  • APIs & Integration
  • Architecture & Process
3 min read

Strava

Upload GPS tracks to Strava from the command line with curl: create an API application, get an OAuth token and call the uploads endpoint.

  • APIs & Integration
3 min read

JSON Web Tokens (JWT)

How access and refresh tokens work in JWT authentication, the endpoints a server needs, and where a client should keep each token.

  • Security
  • APIs & Integration

Type to search the whole site.