37
$.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?

comiventor
  • 3,922
  • 5
  • 50
  • 77
BaconJuice
  • 3,739
  • 13
  • 56
  • 88
  • If you upgrade to jQuery 1.9, it will properly go to the error callback if a json string is not returned. – Kevin B Jan 15 '13 at 19:57
  • 2
    If you instead want to check if an object of the array contains empty data, we would need to see exactly what is being returned in your json string as it may be different depending on server-side language and your method of generating json. – Kevin B Jan 15 '13 at 19:58
  • @KevinB all I get back is a text "null" on console – BaconJuice Jan 15 '13 at 20:00
  • @adeneo tried it. Doesn't work =/ – BaconJuice Jan 15 '13 at 20:02
  • @BaconJuice Where in the console? the console has many different sections and i don't see a console.log() that would produce null. is the responseText for the request `"null"`? or is it `""` – Kevin B Jan 15 '13 at 20:03
  • This may be one of the stupider things I've asked, but wouldn't `json` be null before `d` is? – krillgar Jan 15 '13 at 20:05
  • @KevinB sorry the response text in the console is actually [] just that. again sorry I was a bit confused when I said null – BaconJuice Jan 15 '13 at 20:21
  • u should change correct answere – clockw0rk Sep 28 '18 at 13:48

5 Answers5

75

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");
    }
Arun Pratap Singh
  • 3,428
  • 30
  • 23
61

Just test if the array is empty.

$.getJSON(url,function(json){
    if ( json.length == 0 ) {
        console.log("NO DATA!")
    }
});
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • 9
    There is no "length" property JSON Object has so if you get response with not an Array instance you will end up having "NO DATA!" message all the time even if object is not acctualy empty. How this answer ended up approved and with got more reputation than a CORRECT answer below from Mr. Arun Pratap Singh is a mistery for me. – Soul_man Mar 20 '14 at 09:46
  • @Soul_man Right, but in this case it was clearly an array. *"@KevinB sorry the response text in the console is actually [] just that."* – Kevin B Mar 20 '14 at 14:07
  • 1
    In my case length is undefined – Mohammad Ashfaq Mar 26 '14 at 07:29
  • @Mo.Ashfaq Then you don't have an array. What do you have? – Kevin B Mar 26 '14 at 14:09
  • When I am performing JSON.stringify on my response, I just get {} – Mohammad Ashfaq Mar 27 '14 at 08:44
  • 3
    @amit_kumar yes it does. – Kevin B Aug 16 '17 at 13:44
7
if (!json[0]) alert("JSON empty");
Justin Levene
  • 1,630
  • 19
  • 17
0

You can use $.isEmptyObject(json)

https://api.jquery.com/jQuery.isEmptyObject

  • 1
    This is more or less just repeating [this existing answer](https://stackoverflow.com/a/20262434). – Pang Mar 12 '18 at 04:03
-3
$.getJSON(url,function(json){
if ( json.length == 0 ) 
{
console.log("NO !")
}
});
Ranjith
  • 17
  • 2
  • 4
    Hi, welcome to Stack Overflow. Please check out [how to write good answers](http://meta.stackexchange.com/q/7656). Also, this question has been answered a long time ago, we'll be glad if you devote your time to more [recent questions](http://stackoverflow.com/questions). – Mifeet Aug 20 '15 at 12:24