Javascript - Delete empty properties in an Json object

Delete empty properties of an json object.

1
2
3
4
5
6
7
function removeEmptyProperties(obj) {
Object.keys(obj).forEach(function(key) {
if (obj[key] && typeof obj[key] === 'object') removeEmpty(obj[key]); //recursive for objects
else if (obj[key] == null || obj[key]== "") delete obj[key]; //remove empty properties
if (typeof obj[key] === 'object' && Object.keys(obj[key]).length == 0) delete obj[key]; //remove empty objects
});
};