Last active
September 28, 2022 13:00
-
-
Save thomaswilburn/1cb7c14817a6a7eba82863ebe74a0a70 to your computer and use it in GitHub Desktop.
=importJSON support for Sheets, producing keypath/value pairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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