Empty JSON objects in Node.js

0

I have a query, I would like to know how to validate if a JSON object is empty or not, try doing it with "undefined" or null but since I am new in this I am not sure if this is correct.

This is my code:

var v = {"Mail":"[email protected]","Copy":"", "cdd":"[email protected]"}
if( v.cdd !== undefined){
    if(typeof(v.Mail) === 'string'){
        if(typeof(v.Copy) === 'string' || v.Copy !== ""){
            if(typeof(v.cdd === 'string' || v.cdd !== "")){
                console.log("Todo bien")
            }
        }
    }
}

I do not think it's very good, if you can please tell me if I can thank you for solving it or giving me an example.

Thank you.

    
asked by java005 05.09.2018 в 23:33
source

1 answer

0

var url = {"Mail":"[email protected]","Copy":"", "cdd":"[email protected]"};

function isEmpty(obj) {
for(var key in obj) {
    if(obj.hasOwnProperty(key))
        return false;
}
return true;
}

function GetEmptyProperties(obj) {
for(var key in obj) {
    if(obj.hasOwnProperty(key)){
      if(!obj[key]){
        alert("vacia la propiedad "+key);  
      }
    }            
}
}

if(isEmpty(url)) {
  alert("Tu JSON es vacio");
} else {
  GetEmptyProperties(url);
}
    
answered by 06.09.2018 / 00:16
source