remove keys with empty values from json

125 views
Skip to first unread message

Colen

unread,
Jun 29, 2020, 8:36:16 AM6/29/20
to google-apps-sc...@googlegroups.com

    {
      "topper": [{
        "language": "English",
        "value": ""
      }, {
        "language": "German",
        "value": "hASTA"
      }],
      "bottom": [{
        "language": "English",
        "value": "jfgfjg"
      }, {
        "language": "German",
        "value": "fkjhf"
      }],
      "In": "12 am",
      "Out": "3 am",
      "Type": ""
    }

Hi guys, I want to remove the keys that have empty values, I tried using `filter` but that showed me error in google sheets. 
I want to send only the data to API that has values.

In this case, for 

> language:german, the value is empty so i Would skip sending **topper**. to the API.

the json to be sent:

<!-- language: lang-js -->

    {
      "bottom": [{
        "language": "English",
        "value": "jfgfjg"
      }, {
        "language": "German",
        "value": "fkjhf"
      }],
      "In": "12 am",
      "Out": "3 am"
    }

code used:

    apidata = userdata.filter(function(x) { return x !== "" }); 

Can you please guide me on how to do this?

Alan Wells

unread,
Jun 29, 2020, 8:40:43 AM6/29/20
to Google Apps Script Community
 If an inner element is empty you need to delete the upper most element.
You don't know which keys to remove unless an inner element has an empty value.

    function removeEmptyElements(obj) {
      var i,k,k2,L,outerElmtValue,thirdLevelObj,thirdLvlVal;
     
      for (k in obj) {
        outerElmtValue = obj[k];
       
        if (!outerElmtValue) {//There is no truthy value in this element
          delete obj[k];//Delete the outer element
          continue;
        }
       
        if (Array.isArray(outerElmtValue)) {
          L = outerElmtValue.length;
         
          for (i=0;i<L;i++) {
            thirdLevelObj = outerElmtValue[i];
           
           
            for (k2 in thirdLevelObj) {
              thirdLvlVal = thirdLevelObj[k2];
              Logger.log('thirdLvlVal: ' + thirdLvlVal)
             
              if (!thirdLvlVal) {
                delete obj[k];//Delete the parent
              }
            }
          }
        }
      }
     
      return obj;
    }
   
    function runTest() {
     
      var testObj =
          {
            "top": [{

              "language": "English",
              "value": ""
            }, {
              "language": "German",
              "value": "hASTA"
            }],
            "bottom": [{
              "language": "English",
              "value": "jfgfjg"
            }, {
              "language": "German",
              "value": "fkjhf"
            }],
            "In": "12 am",
            "Out": "3 am",
            "Type": ""
          };
     
     
      var finalObj = removeEmptyElements(testObj);
     
      Logger.log(JSON.stringify(finalObj))
    }

Bruce Mcpherson

unread,
Jun 30, 2020, 12:58:43 PM6/30/20
to Google Apps Script Community
<!-- language: lang-js -->
const data = {
      "topper": [{
        "language": "English",
        "value": ""
      }, {
        "language": "German",
        "value": "hASTA"
      }],
      "bottom": [{
        "language": "English",
        "value": "jfgfjg"
      }, {
        "language": "German",
        "value": "fkjhf"
      }],
      "In": "12 am",
      "Out": "3 am",
      "Type": ""
    };
const cleaned = Object.keys(data)
.reduce ( (p,c) => {
  if(data[c] && (!Array.isArray(data[c]) || data[c].every(f=>f.value))) p[c] = data[c];
  return p;
}, {}); 
Reply all
Reply to author
Forward
0 new messages