Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Last active September 28, 2022 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswilburn/1cb7c14817a6a7eba82863ebe74a0a70 to your computer and use it in GitHub Desktop.
Save thomaswilburn/1cb7c14817a6a7eba82863ebe74a0a70 to your computer and use it in GitHub Desktop.
=importJSON support for Sheets, producing keypath/value pairs
function IMPORTJSON(url) {
var response = UrlFetchApp.fetch(url);
var text = response.getContentText();
var json = JSON.parse(text);
var output = [];
var step = function(at, value) {
if (value instanceof Array) {
for (var i = 0; i < value.length; i++) {
var key = `${at}[${i}]`;
step(key, value[i]);
}
} else if (typeof value == "object") {
for (var k in value) {
var key = at ? at + "." + k : k;
step(key, value[k]);
}
} else {
output.push([at, value]);
}
}
step("", json);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment