Reputation: 77
I am requesting an API and the json i am getting back is this
{
"items":[
{
"id":"231321",
"externalId":32131",
"status":"published",
"network":"facebook",
"message":"message",
"externalChannels":[
"231312"
],
"pictures":[
picture"
],
"type":"picture",
"facebook":{
"dark":false
},
"labels":[
"bus",
"train",
"car"
],
When i try to parse the labels part though i only get the first value and not the other too. I have done some googling and found it is trailing commas but i was not able to find any solutions. Any thoughts?
EDIT: Using the script below i can see on the logger all the values of the labels but only the first is written on the spreadsheet.
function getdata() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('api');
var range = sheet.getRange("A:C");
var response = UrlFetchApp.fetch("api call");
var dataAll = JSON.parse(response.getContentText());
var dataSet = dataAll.items;
var rows = [],
data;
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
rows.push([new Date(),data.labels]); //your JSON entities here
}
Logger.log(rows)
//sheet.getRange(getlastRow() + 1, 1, rows.length, 2).setValues(rows);
sheet.getRange(sheet.getLastRow() + 1, 1, rows.length, 2).setValues(rows);
Upvotes: 1
Views: 207
Reputation: 26806
Sample:
function myFunction() {
var myObject = {
"items":[
{
"id":"231321",
"externalId":"32131",
"status":"published",
"network":"facebook",
"message":"message",
"externalChannels":[
"231312"
],
"pictures":[
"picture"
],
"type":"picture",
"facebook":{
"dark":false
},
"labels":[
"bus",
"train",
"car"
],
}
]
}
var data = myObject.items;
var array = [];
for (var i = 0; i< data.length; i++){
var newJson = Object.keys(data[i]).map(function (key) {
return [key, data[i][key]];
});
for(var j = 0; j< newJson.length; j++){
array.push(newJson[j]);
}
}
Logger.log(array);
Logger.log(array[0][0] + " is " + array[0][1]);
}
Upvotes: 1