I'm quite beginner with Apps Script / Javascript. I have some JSON data that I'm exporting using an API.
Below is what I get when I do: console.log(jsonData);
I'm trying to:
- Transform this JSON data into CSV.
- Put the data in the first sheet of my active spreadsheet
Basically it should look like this:
However I'm stuck at this step and I cannot get the next piece of code right ... (how to transform into CSV and putting the data in a Google sheet).
Below is the beginning of my script:
function myFunction() {
var apiKey = "MyApiKEY";
var now = new Date();
var startDate = new Date(now.setDate(now.getDate()-2))
var endDate = startDate
var startDateFormatted = Utilities.formatDate(new Date(startDate), "GMT+7", "yyyy-MM-dd");
var endDateFormatted = Utilities.formatDate(new Date(endDate), "GMT+7", "yyyy-MM-dd");
var url = 'https://public-api.vendor.com/v1/clicks?start_date='+ startDateFormatted + '&end_date=' + endDateFormatted;
var options1 = {
"method": "get",
"headers": {
"accept": "application/json",
"Authorization": apiKey
}
}
var response = UrlFetchApp.fetch(url, options1);
var jsonData = JSON.parse(response.getContentText());
// console.log(jsonData);
}
I would appreciate any help in getting those data into a Google Sheet. Also feel free to modify anything in the beginning of my script if you think something is wrong or unnecessary.