Ted
Ted

Reputation: 77

Cant parse all JSON values in google apps script (trailing commas)

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

Answers (1)

ziganotschka
ziganotschka

Reputation: 26806

The problem is not the trailing comma, but the structure of your json - you have an object inside of an object

  • If your json looks look described in your question (I assume the lacking brackets and quotes are typing mistakes), it is no a string, but already a valid JSON object which does not need to be parsed.
  • Indeed, trying to parse a non-string will give you an error
  • If you want to access the individual elements of your JSON object, you can e.g. push them into an array separated by key a and value
  • This can be conveniently done with Object.keys

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

Related Questions