$.getJSON(url, function(json) {
var output = '';
$.each(json, function(i,d) {
if(d.DESCRIPTION == 'null'){
console.log("Its empty");
}
var description = d.DESCRIPTION;
output += '<tr><td>'+d.NAME+'</td><td>'+'<tr><td>'+d.DESCRIPTION+'</td><td>';
});
});
I tried adding the
if(d.DESCRIPTION == 'null'){ console.log("Its empty");
to check if the object returned is empty, but it doesn't work.
Can someone explain to me what's wrong with this?
This question is related to
javascript
jquery
ajax
json
You can use $.isEmptyObject(json)
$.getJSON(url,function(json){
if ( json.length == 0 )
{
console.log("NO !")
}
});
if (!json[0]) alert("JSON empty");
Below code(jQuery.isEmptyObject(anyObject) function is already provided) works perfectly fine, no need to write one of your own.
// works for any Object Including JSON(key value pair) or Array.
// var arr = [];
// var jsonObj = {};
if (jQuery.isEmptyObject(anyObjectIncludingJSON))
{
console.log("Empty Object");
}
Source: Stackoverflow.com