Recipes / Recipe 12
Import JSON from an API into Google Sheets on a schedule
Pull data from any JSON API (your store, CRM, or a public dataset) into a tab, flattening nested fields, refreshed hourly.
You'll need: An API URL that returns JSON. The demo uses a free public test API, so it works as-is.
Steps
- Extensions → Apps Script, paste the script, and run
importJson. You'll get an API import tab with 10 demo users. - Swap
urlfor your API. If the response wraps the list (e.g.{"data": [...]}), setlistKey: 'data'. - List the
fieldsyou want; use dots for nested values likeaddress.city. - Needs an API key? Go to Project Settings → Script properties, add e.g.
API_KEY, and setapiKeyProperty: 'API_KEY'. Never paste keys into the code. - Run
installHourlyImportto refresh it automatically.
/**
* Pull JSON from an API into a sheet, on demand or on a schedule.
* The demo URL is a free public test API, so you can try it before pointing it at your own.
*/
const API_CONFIG = {
url: 'https://jsonplaceholder.typicode.com/users',
listKey: '', // if the API returns {"data": [...]}, put 'data' here
fields: ['id', 'name', 'email', 'address.city', 'company.name'], // dot paths for nested values
sheetName: 'API import',
apiKeyProperty: '' // e.g. 'API_KEY': stored in Project Settings > Script properties, never in code
};
/** Pure: read a nested value like "address.city". */
function getPath(obj, path) {
return path.split('.').reduce((o, k) => (o !== null && o !== undefined ? o[k] : undefined), obj);
}
/** Pure: turn an array of objects into rows with a header. */
function toRows(items, fields) {
const body = items.map((item) => fields.map((f) => {
const v = getPath(item, f);
if (v === null || v === undefined) return '';
return typeof v === 'object' ? JSON.stringify(v) : v;
}));
return [fields].concat(body);
}
function importJson() {
const cfg = API_CONFIG;
const headers = {};
if (cfg.apiKeyProperty) {
const key = PropertiesService.getScriptProperties().getProperty(cfg.apiKeyProperty);
headers.Authorization = 'Bearer ' + key;
}
const res = UrlFetchApp.fetch(cfg.url, { headers: headers, muteHttpExceptions: true });
if (res.getResponseCode() !== 200) {
throw new Error('API returned ' + res.getResponseCode() + ': ' + res.getContentText().slice(0, 200));
}
const data = JSON.parse(res.getContentText());
const items = Array.isArray(data) ? data : (getPath(data, cfg.listKey) || []);
const rows = toRows(items, cfg.fields);
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(cfg.sheetName) || ss.insertSheet(cfg.sheetName);
sheet.clearContents();
sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
sheet.setFrozenRows(1);
}
function installHourlyImport() {
ScriptApp.newTrigger('importJson').timeBased().everyHours(1).create();
}
Good to know
- The script sends the key as
Authorization: Bearer …, which most APIs use. Some want a different header or a query parameter; check your API's docs. - Many APIs return results in pages (e.g. 100 at a time). Fetching every page needs a small loop. That's a typical Quick fix.
- Arrays and objects you don't list as dot paths are written as JSON text so nothing is silently lost.
Do it yourself
Recipe Club
Prefer to set things up yourself? Join the Recipe Club and get access to a private GitHub repository of ready-made, tested automations for Sheets, Apps Script, Python and Node. Each one comes with setup notes and sample data.
- A new recipe added every week
- Everything in the free library, plus the longer, more complete versions
- Use them in your own business as much as you like
- Cancel anytime by email (or the Stripe customer portal once available)
Recipe Club opens soon: join the waitlist
You'll need a free GitHub account. After checkout, reply with your GitHub username and we'll send the repo invite, usually within one business day.
Or buy a one-time recipe pack
- Clean Data Pack
Dedupe, cleaning, merging and normalizing scripts and formulas for messy exports, with a sample sheet.
Coming soon - Forms & Inbox Pack
Form auto-replies, request routing, a lead log and follow-up nudges, ready to paste into Apps Script.
Coming soon - Money Ops Pack
Invoice reminders, a monthly revenue dashboard with QUERY, a receipts log and nightly backups.
Coming soon
Written with AI assistance. Script logic is unit-tested; formulas follow documented Google Sheets behavior. Always try new automations on a copy of your sheet first.