Created
March 25, 2021 22:48
-
-
Save miguelmota/583757335da9f77362966174cf4377c8 to your computer and use it in GitHub Desktop.
Google sheets import JSON script
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
/** | |
* Imports JSON data to your spreadsheet | |
* @param url URL of your JSON data as string | |
* @param xpath simplified xpath as string | |
* @customfunction | |
*/ | |
function ImportJSON(url, xpath) { | |
try { | |
// /rates/EUR | |
var res = UrlFetchApp.fetch(url); | |
var content = res.getContentText(); | |
var json = JSON.parse(content); | |
var patharray = xpath.split("."); | |
//Logger.log(patharray); | |
for (var i = 0; i < patharray.length; i++) { | |
json = json[patharray[i]]; | |
} | |
//Logger.log(typeof(json)); | |
if (typeof(json) === "undefined") { | |
return "Node Not Available"; | |
} else if (typeof(json) === "object") { | |
var tempArr = []; | |
for (var obj in json) { | |
tempArr.push([obj, json[obj]]); | |
} | |
return tempArr; | |
} else if (typeof(json) !== "object") { | |
return json; | |
} | |
} catch (err) { | |
return "Error getting data"; | |
} | |
} |
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
=ImportJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin", "0.current_price") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment