Matt Smith
Matt Smith

Reputation: 2120

Checking for empty data in JSON in JavaScript

I'm working with an API call that's returning JSON and in some scenarios some of the data is empty. For example, the snippet below shows that roleBusinessScopes is empty.

{
    "userProfile": {
        "organizationContacts": [
            {
                "roleBusinessScopes": {}
            }
        ]
    }
}

I wanted to be able to check if roleBusinessScopes is empty. I tried roleBusinessScopes.length = 0, however, that doesn't work.

When roleBusinessScopes does return data...

"roleBusinessScopes": {
    "businessScopes": {
        "scopeName": "something"
    }
}

... I can check for that:

if (organizationContacts[i].roleBusinessScopes.businessScopes[0].scopeName !== "something") 
{
    // do something
}

How can I check if roleBusinessScopes has no data?

Upvotes: 0

Views: 56

Answers (6)

JonoJames
JonoJames

Reputation: 1223

What I would use is a function to check it as there are multiple possibilities .

Sometimes a server will write null in its place so alternative checks need to be made

Us this function

 function checkempty(jsonString) { 
        if (jsonString == null ||  
            jsonString == undefined || 
            jsonString.length == 0) { 

            console.log("Name cannot be empty\n"); 
            return false; 
        } else { 
            console.log("Your response has been recorded\n"); 
            return true; 
        } 
    } 
 checkempty(Object.keys(organizationContacts[i].roleBusinessScopes))

Upvotes: 0

Alessio Cantarella
Alessio Cantarella

Reputation: 5201

Assuming the structure is always the same (and that you're in a ideal world where you don't need to check the validity of each property and child object), you could just check if businessScopes is not undefined.

let objectWithoutRoleBusinessScopes = {
  "userProfile": {
    "organizationContacts": [{
      "roleBusinessScopes": {}
    }]
  }
};
let objectWithRoleBusinessScopes = {
  "userProfile": {
    "organizationContacts": [{
      "roleBusinessScopes": {
        "businessScopes": {
          "scopeName": "something"
        }
      }
    }]
  }
};
function hasRoleBusinessScopes(objectToTest) {
  return objectToTest.userProfile.organizationContacts[0].roleBusinessScopes.businessScopes != undefined;
}
console.log(hasRoleBusinessScopes(objectWithoutRoleBusinessScopes));
console.log(hasRoleBusinessScopes(objectWithRoleBusinessScopes));

Upvotes: 0

sudo97
sudo97

Reputation: 914

You could simply check if it is empty or not by this statement


if(organizationContacts[i].roleBusinessScopes === {}){
    // handle empty case here
} else {
    // handle non-empty case here
}

Upvotes: 0

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22875

You can use Object.keys(obj).length > 0 to check if object has some keys (data) or not

if (Object.keys(organizationContacts[i].roleBusinessScopes).length > 0) {
   // Not empty

} else {
  // empty

}

Upvotes: 2

Wes Harper
Wes Harper

Reputation: 172

You should be able to do something like

if (Object.entries(organizationContacts[i].roleBusinessScopes).length === 0) {
  // do something
}

Object.entries() will return a list of all the object's properties, which you can use to key in on length.

Upvotes: 0

Japsz
Japsz

Reputation: 785

You could try using Object.keys({your_object_here...}) and check the length of the returned array.

Upvotes: 0

Related Questions