0

I am using google spread-sheet as a database for my application. I want that spreadsheet data to return as a JSON response like GET API. I have written the below code for getting the response.

function doGet(request){
  // Open Google Sheet using ID
  var sheet = SpreadsheetApp.openById("sheetid");

  // Get all values in active sheet
  var values = sheet.getActiveSheet().getDataRange().getValues();
  var data = [];

  for (var i = 1; i <= values.length-1; i++) {
    var row = values[i];
        var feedback = {};
    feedback['timestamp'] = row[1];
    feedback['name'] = row[2];
    feedback['designation'] = row[3];
    feedback['blood_group'] = row[4];
  
    data.push(feedback);
  }
  return ContentService
  .createTextOutput(JSON.stringify(data))
  .setMimeType(ContentService.MimeType.JSON);
}

I want my response like this :

[{
"timestamp": "2020-08-03T12:43:38.662Z",
"name": "Mehedi Hasan",
"designation": "developer",
"blood_group": "o-"
}]

but, it's giving response like this:

[{
timestamp: "2020-08-03T12:43:38.662Z",
name: "Mehedi Hasan",
designation: "developer",
blood_group: "o-"
}]

It's returning Javascript object not in JSON string even though I have used JSON.stringify() function to convert it. I don't understand if there is any mistake I have done here? Any suggestion will be helpful.

Juthi Sarker Aka
  • 2,217
  • 6
  • 16
  • 22
  • How are you testing this? – Diego Sep 22 '20 at 15:36
  • @Diego I am publishing it as web app and generated a dummy spreadsheet for this. – Juthi Sarker Aka Sep 22 '20 at 15:45
  • After defining `feedback` in your code, I see that it is already returning the response you want, so I'm not sure what you're doing differently. Please also check [this answer](https://stackoverflow.com/a/2904181/1329498) to understand more about JSON string vs object (i.e. no such thing as a JSON object). – Diego Sep 22 '20 at 16:04
  • @Diego I am doing the same. But actually it returning the javascript object..not the JSON string. – Juthi Sarker Aka Sep 22 '20 at 16:19
  • @JuthiSarkerAka I am getting the desired output. Did you specify `var feedback = {};` before the for loop? because I don't see it anywhere in your code. As a proof: http://prntscr.com/ulxsip – Marios Sep 22 '20 at 16:37
  • @Marios I have edited the code. ```var feedback = {};``` I have initialized this in for loop...still getting the same result – Juthi Sarker Aka Sep 22 '20 at 16:49
  • Have you **updated** your webapp to a new version ? Maybe you changed something in the code and the changes haven't been reflected in the resulting webpage yet? – Marios Sep 22 '20 at 16:51
  • Also put the `feedback` definition **before** the for loop, **not inside** the for loop. – Marios Sep 22 '20 at 16:52
  • @JuthiSarkerAka here as how you can update your webapp: http://prntscr.com/uly77z – Marios Sep 22 '20 at 16:59
  • 1
    Both responses are the same. JSON stands for JavaScript object notation. Quotes may or may not be seen depending on the viewer you're using. There is no issue – TheMaster Sep 22 '20 at 20:50
  • JuthiSarkerAka can you confirm @TheMaster comment? – Jose Vasquez Sep 23 '20 at 08:53

0 Answers0