3

I use Jquery to check if my object from an ajax call is empty or not.

In this example I have made a correct AJAX call and it returns some data.

console.log ("obj before Json parse: ",response);
var test = $.isEmptyObject(response);
console.log("test if object is empty:",test);

obj before Json parse:  [{"dateTime":"2015-10-02","entries":220}]
est if object is empty: false

However in this example I have made an incorrect AJAX call that returns nothing.

console.log ("obj before Json parse: ",response);
var test = $.isEmptyObject(response);
console.log("test if object is empty:",test);

obj before Json parse:  []
test if object is empty: false

surely the test variable should be true in this case as the object is empty?

  • 1
    Possible duplicate of [How do I test for an empty Javascript object?](http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object) – Diptox Oct 12 '15 at 10:54
  • 1
    `isEmptyObject` should only be used on plain objects, you seem to have an array, and could just do `response.length` instead. – adeneo Oct 12 '15 at 10:54

2 Answers2

7

Use length to check if the object is empty or not.

var isEmpty = (response || []).length === 0;
Tushar
  • 78,625
  • 15
  • 134
  • 154
  • I should probably use response.length; then. Because I just tested isPlainObject and it returned false in both cases. – Andreas Uldall Leonhard Oct 12 '15 at 10:59
  • @AndreasUldallLeonhard `isPlainObject` works on Objects, if the response is array, use `arr.length` to check if it is empty – Tushar Oct 12 '15 at 11:00
  • `response.length` will throw an error if `response` is `null` or `undefined`. You could be more defensive and have `var test = response && response.length`. – user5325596 Oct 12 '15 at 11:01
  • @user5325596 Thanks for the case, I've updated answer – Tushar Oct 12 '15 at 11:02
  • Hmm this makes no sense at all obj before Json parse: [{"dateTime":"2015-10-02","entries":220}] index.html:335 test if object is empty: 41 obj before Json parse: [] test size of object: 2 That's what happened when I did .length on both – Andreas Uldall Leonhard Oct 12 '15 at 11:09
  • @AndreasUldallLeonhard Because that's not JSON, that is just a _string_, so the size 2 – Tushar Oct 12 '15 at 11:10
  • So I should check if the size is equal to 2 instead of equal to 0 as your answer suggest? – Andreas Uldall Leonhard Oct 12 '15 at 11:16
  • @AndreasUldallLeonhard No, convert the string to JSON and check its length `JSON.parse(str).length;` For example `JSON.parse('[]').length` returns **0** – Tushar Oct 12 '15 at 11:18
  • I see so the error is somewhere in my AJAX call? Because I know for a fact that if I put in the right information in the AJAX call it get a JSON returned. I have a php class where I encode all the data I get from my database to JSON. If the data I search for does not exist in my database, then that's the case where I get the empty array. My first example of [{"dateTime":"2015-10-02","entries":220}] is a legit JSON object. I decode it later in my code with obj = JSON.parse(response); without any problems – Andreas Uldall Leonhard Oct 12 '15 at 11:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92034/discussion-between-tushar-and-andreas-uldall-leonhard). – Tushar Oct 12 '15 at 11:20
-1
var jsonData = JSON.parse(responseBody);
tests['empty_or_not'] = jsonData.length === 0;
Yaseen Ahmad
  • 1,769
  • 5
  • 21
  • 41
Madhusudhan R
  • 235
  • 4
  • 17