0

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?

1
  • Are you actually use if (roleBusinessScopes.length = 0)? so you miss = there. check it with if (roleBusinessScopes.length == 0) or if (roleBusinessScopes.length > 0) Commented Jan 21, 2020 at 23:49

6 Answers 6

2

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

}

Sign up to request clarification or add additional context in comments.

Comments

0

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));

Comments

0

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

Comments

0

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.

Comments

0

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
}

Comments

0

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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.