Google Sheets as a JSON data source
When you need a read-only JSON data source that is very simple for users to maintain, consider using a Google Sheet. It’s not very obvious that you can get JSON from a sheet, but it can be done in 4 steps.
1. Get the ID of your sheet
The long number seen in the URL while you are editing
e.g.
https://docs.google.com/spreadsheets/d/1bPW98SzQ5SRsincyVGdP3ctM8ey3oSpncnyo9ASFUDM/edit#gid=0
yourSheetID = 1bPW98SzQ5SRsincyVGdP3ctM8ey3oSpncnyo9ASFUDM
(this is the ID for my sheet, not yours)
2. Publish your sheet as a web page
File > Publish to the web...
The default options are fine.
Check the link created when published to view your sheet as read-only.
3. Get the ID(s) for the desired tab(s)
Manually build a URL with the following pieces
https://spreadsheets.google.com/feeds/worksheets/{yourSheetID}/public/full?alt=json
That will give you JSON like this which contains an ID for each tab in your sheet
The ID for each of the tabs can be found after the final slash in these values
feed.entry[0].id.$t
feed.entry[1].id.$t
feed.entry[3].id.$t
etc.
the IDs for my 4 tabs
omyavzt (Dogs)
o9ws5hl (Cats)
od6 (AllOthers)
o97vx72 (Notes)
Tip: The JSON will be formatted pretty if you view it in FireFox.
4. A JSON link for EACH tab
Manually build a URL with the following pieces
https://spreadsheets.google.com/feeds/list/{yourSheetID}/{yourTabID}/public/full?alt=json
That will give you JSON like this for the Dogs tab which contains the data in one tab.
Cats tab
AllOthers tab
Notes
- It may take up to 5 minutes to see published changes made in the sheet. Usually happens in a few seconds.
- The IDs for the sheet & tabs never changes. Even if they are renamed.
- Somehow it’s smart enough to know the first row is the header.
(I used: view > freeze > 1 row on the first row ) - Any data under the first empty row will not show in the JSON.
- feed.entry will always contain a record for each row in your tab.
- If the user renames a header, that MAY cause issues if you go after attributes by name.
- JSON attributes are all lower case & case sensitive. No matter what case you use in your sheet.
- The email you use to log into Google will be exposed in the JSON.
- If you will be using this source for high volume, I do not know if there are use limits.