JSFiddle - React, Tailwind, and code Playground

by jfriend00

CSS

body {
    font-family: "Courier New"; 
    font-size: 11px;
}

JavaScript

// make sure the ES5 Array.isArray() method exists
if(!Array.isArray) {
  Array.isArray = function (arg) {
    return Object.prototype.toString.call(arg) == '[object Array]';
  };
}

function removeEmptyArrays(data) {
    for (var key in data) {
        var item = data[key];
        // see if this item is an array
        if (Array.isArray(item)) {
            // see if the array is empty
            if (item.length == 0) {
                // remove this item from the parent object
                delete data[key];
            }
        // if this item is an object, then recurse into it 
        // to remove empty arrays in it too
        } else if (typeof item == "object") {
            removeEmptyArrays(item);
        }
    }    
}

var jsonOBJ = {
    "B": {
        "1":"90",
        "2": "200",
        "3": [], 
        "C": [], 
        "D": {
            "A": "foo", 
            "B": []
        }
    }
};
document.body.innerHTML = "Input:<br>" + JSON.stringify(jsonOBJ);
removeEmptyArrays(jsonOBJ);
document.body.innerHTML += "<br><br>Output:<br>" + JSON.stringify(jsonOBJ);