Reputation: 4099
I am trying to import JSON data from IEXAPI into Google Sheets, and have found the following function here:
function IMPORTJSON(url,xpath){
try{
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);
var patharray = xpath.split("/");
for(var i=0;i<patharray.length;i++){
json = json[patharray[i]];
}
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";
}
}
The JSON that I'm trying to read is:
[{
"symbol":"AAPL",
"sector":"electronictechnology",
"securityType":"cs",
"bidPrice":0,
"bidSize":0,
"askPrice":0,
"askSize":0,
"lastUpdated":1587067200000,
"lastSalePrice":286.48,
"lastSaleSize":100,
"lastSaleTime":1587067199848,
"volume":357754
}]
But when I call the function in Google Sheets I get the error Node Not Available
:
=IMPORTJSON("https://cloud.iexapis.com/stable/tops?token=MY_API_KEY&symbols=aapl", "symbol")
I don't understand what is wrong, because symbol
is a rootnode in the JSON. Can somebody please explain?
Upvotes: 0
Views: 160
Reputation: 15268
Your object is wrapped in an array.
json = json[0][patharray[i]];
Upvotes: 1