How To Trim Google Apps Script Output

140

When I saw your script, dataArray is put to the property of user by jo.user = dataArray;. By this, result is {"user":[]dataArray}.

As a simple modification for resolving the issue, please modify as follows and test it.

From:

jo.user = dataArray;

To:

jo = dataArray;

By this, result is only dataArray.

Note:

  • It seems that you are using Web Apps. So please redeploy the Web Apps as new version, when the script is modified. By this, the latest script is reflected to Web Apps. Please be careful this.
Share:
140
codeNameRayven
Author by

codeNameRayven

Updated on December 21, 2022

Comments

  • codeNameRayven
    codeNameRayven over 1 year

    I'm "developing" a mobile app in Flutter that must get a json via httpService for parsing. I don't know enough about how Flutter parses json files, to reconfigure the methods and/or functions of my app, allowing it to use what my Google App Script currently outputs; and i dont know enough about Google App Scripts to mod that code so that it provides the Flutter app with the format it requires. But it seems like doing one or the other would solve my issue.

    Specifically, my Flutter app expects a json that looks like this:

    [
    {   "userId":"My Category Kittens","id":"Black","title":"Solid","body":"10"},
    {   "userId":"My Category Kittens","id":"White","title":"Striped","body":"20"},
    {   "userId":"My Category Kittens","id":"Gray","title":"Spotted","body":"30"},
    {   "userId":"My Category Puppies","id":"Brown","title":"Shaggy","body":"40"},
    {   "userId":"My Category Puppies","id":"Blue","title":"Scruffy","body":"50"},
    {   "userId":"My Category Puppies","id":"Orange","title":"Mangey","body":"60"},
    {   "userId":"My Category Puppies","id":"Pink","title":"Fluffy","body":"70"},
    {   "userId":"My Category Birdies","id":"Green","title":"Slick","body":"80"},
    {   "userId":"My Category Birdies","id":"Yellow","title":"Pixeled","body":"90"},
    {   "userId":"My Category Birdies","id":"Purple","title":"Plaid","body":"100"},
    {   "userId":"My Category Birdies","id":"Transparent","title":"Argiled","body":"110"},
    {   "userId":"My Category Birdies","id":"Utraviolet","title":"Bald","body":"120"}
    ]
    

    But my Google Apps Script produces this:

    {"user":[
    {   "userId":"My Category Kittens","id":"Black","title":"Solid","body":"10"},
    {   "userId":"My Category Kittens","id":"White","title":"Striped","body":"20"},
    {   "userId":"My Category Kittens","id":"Gray","title":"Spotted","body":"30"},
    {   "userId":"My Category Puppies","id":"Brown","title":"Shaggy","body":"40"},
    {   "userId":"My Category Puppies","id":"Blue","title":"Scruffy","body":"50"},
    {   "userId":"My Category Puppies","id":"Orange","title":"Mangey","body":"60"},
    {   "userId":"My Category Puppies","id":"Pink","title":"Fluffy","body":"70"},
    {   "userId":"My Category Birdies","id":"Green","title":"Slick","body":"80"},
    {   "userId":"My Category Birdies","id":"Yellow","title":"Pixeled","body":"90"},
    {   "userId":"My Category Birdies","id":"Purple","title":"Plaid","body":"100"},
    {   "userId":"My Category Birdies","id":"Transparent","title":"Argiled","body":"110"},
    {   "userId":"My Category Birdies","id":"Utraviolet","title":"Bald","body":"120"}
    ]}
    

    So, any suggestions for editing my Google Apps Script to omit the leading '{"user":', and the trailing '}' ?

    Here is my Google App Script code:

    function doGet(e){
    
     // Spread Sheet url
     var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1jsBS-RBNRxYU66WFkJHvrzHLGmNqxBzzQfaHJO6i6UY/edit#gid=446843772");
    
    // Sheet Name
     var sheet = ss.getSheetByName("Users");
    
     return getUsers(sheet); 
    
    }
    
    
    function getUsers(sheet){
      var jo = {};
      var dataArray = [];
    
    // collecting data from 2nd Row , 1st column to last row and last column
      var rows = sheet.getRange(2,1,sheet.getLastRow()-1, sheet.getLastColumn()).getValues();
    
      for(var i = 0, l= rows.length; i<l ; i++){
        var dataRow = rows[i];
        var record = {};
        record['userId'] = dataRow[0];
        record['id'] = dataRow[1];
        record['title'] = dataRow[2];
        record['body'] = dataRow[3];
    
        dataArray.push(record);
    
      }  
    
      jo.user = dataArray;
    
      var result = JSON.stringify(jo);
    
      return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
    
    }
    

    Any help is greatly appreciated! Thanks

    • Tanaike
      Tanaike almost 4 years
      In your script, dataArray is put to the property of user by jo.user = dataArray;. In this case, as a simple modification, how about modifying jo.user = dataArray; to jo = dataArray;? By this, only dataArray is used. By the way, it seems that you are using Web Apps. So please redeploy the Web Apps as new version, when the script is modified. By this, the latest script is reflected to Web Apps. Please be careful this.
    • codeNameRayven
      codeNameRayven almost 4 years
      Wow thanks! that accomplished exactly what I wanted. brilliant! unfortunately, i only know now, its not exactly what i needed lol. still, it's a huge step forward for me. anyway, i'm getting a diferent error now: Exception has occurred. FormatException (FormatException: Unexpected character (at line 2, character 1) <!DOCTYPE html> ^ ). might this be because my original json request used a url that ended with '.json' but this google app script url does not indicate file type?
    • Tanaike
      Tanaike almost 4 years
      Thank you for replying and testing it. I'm glad your issue was resolved. When you issue was resolved, can you post it as an answer? By this, it will help users who have the same issue.
    • codeNameRayven
      codeNameRayven almost 4 years
      yes. i will post as answer. should i create a separate question regarding the subsequent error then?
    • Tanaike
      Tanaike almost 4 years
      Now I noticed that your replying had been updated. I apologize for this. From your updated replying, it was found that your initial issue was resolved. About your new issue, I have to apologize for my poor skill. Unfortunately, I cannot understand about it from your replying. I think that providing more information will help to think of new issue. So how about posting your new issue as new question by including more information? By this, I think that it will help users including me think of new issue.
    • codeNameRayven
      codeNameRayven almost 4 years
      Ok. sorry i'm new to stackoverflow. how do i accept your first comment as an answer? i dont see a checkmark
    • Tanaike
      Tanaike almost 4 years
      Thank you for replying. It's no problem. I apologize that I cannot resolve your new issue, soon. In order to understand various situations and, I would like to study more. About accepting the answer, I could understand about it. I will post it as an answer soon. By this, you can accept it.
    • Tanaike
      Tanaike almost 4 years
      I posted it just now. Could you please confirm it?
  • codeNameRayven
    codeNameRayven almost 4 years
    Thanks! this was great help! now, on to the next issue lol
  • Tanaike
    Tanaike almost 4 years
    @hereiis ishow Thank you for replying. About your new issue, I think that providing more information of it will help users including me think of the solution.
  • codeNameRayven
    codeNameRayven almost 4 years
    no prob. Here is a link to my followup issue: stackoverflow.com/questions/62486506/…
  • Tanaike
    Tanaike almost 4 years
    @hereiis ishow Thank you for replying. I would like to check it. When I could understand about it and find the solution, I would like to post an answer.