Reputation: 47
I adapted some code that I found that retrieves data from a url (https://www.lme.com/api/trading-data/day-delayed?datasourceId=9ca4cc7d-4812-479f-8068-23a9053921c1) in json format and pastes it to a Google Sheet.
It seems to be retrieving all the headers, but it is missing the data for all the contracts and prices (you can see it if you open the url). I am wondering what I am doing wrong. The code is below:
function getJSON() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var url="https://www.lme.com/api/trading-data/day-delayed?datasourceId=9ca4cc7d-4812-479f-8068-23a9053921c1"; // JSON URL
var response = UrlFetchApp.fetch(url);
var dataAll = JSON.parse(response.getContentText());
var rows = [Object.keys(dataAll)]; // Retrieve headers.
var temp = [];
for (var i = 0; i < rows[0].length; i++) {
temp.push(dataAll[rows[0][i]]); // Retrieve values.
}
rows.push(temp);
sheet.getRange(1,1,rows.length,rows[0].length).setValues(rows); // Put values to Spreadsheet.
}
Upvotes: 1
Views: 2197
Reputation: 201643
In your situation, how about the following modified script?
function getJSON() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var url = "https://www.lme.com/api/trading-data/day-delayed?datasourceId=9ca4cc7d-4812-479f-8068-23a9053921c1"; // JSON URL
var response = UrlFetchApp.fetch(url);
var dataAll = JSON.parse(response.getContentText());
var rows = dataAll.Rows;
var keys = Object.keys(rows[0]);
var values = [keys, ...rows.map(o => keys.map(k => o[k] && Array.isArray(o[k]) ? o[k][0] : o[k]))];
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
Rows
are retrieved and put to the sheet.When this script is run, the following result is obtained.
Yes the value of the Rows would be sufficient
, I proposed the above modification. But, if you want to add more values, please tell me.Upvotes: 2