1

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);

1 Answer 1

1

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]);
}

Sign up to request clarification or add additional context in comments.

2 Comments

hey, thanks for the response. i already have a script and in the logger i can see all the label values, but only one is written on the spreadsheet. I will attach my script in the original post in case you can find out what's wrong. thanks a lot @ziganotschka
I just had a look at your updated code: Unlike in my case with two nested for loops, you have only one. So you do not loop through all array elements separately and your outcome is: ` [[Tue May 26 10:06:07 GMT+02:00 2020, [bus, train, car]]]` (a 3D array, while setValues() expects a 2D array). If you want to covert the array elements into a string, you can use join(), so: rows.push([new Date(),data.labels.join()]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.