13

In the code below, JSONObject.length is 2:

var JSONObject = [{
    "name": "John Johnson",
    "street": "Oslo West 16",
    "age": 33,
    "phone": "555 1234567"
}, {
    "name": "John Johnson",
    "street": "Oslo West 16",
    "age": 33,
    "phone": "555 1234567"
}];

However, in the code below, JSONObject.length is undefined. Why?

var JSONObject = {
    "name": "John Johnson",
    "street": "Oslo West 16",
    "age": 33,
    "phone": "555 1234567"
};
quietmint
  • 13,885
  • 6
  • 48
  • 73
user2672420
  • 303
  • 2
  • 4
  • 14
  • 7
    Welcome to StackOverflow! Let me give you a few suggestions on how to make this question better for others to answer. First, I'm pretty sure you are missing code: the code you have now does not give any such error. Second, please format you code: you can do this by selecting your code then hitting the "Code Sample" button in the icon bar above the text box (it looks like Grammin has done this for you already). Finally, make sure your question includes the code sample, the error you are getting, and the EXPECTED OUTPUT. Please read [Ask] for more info. – Mark Hildreth Sep 26 '13 at 15:58
  • 1
    *"`var JSONObject = ...`"* That's not a JSON object, it's a JavaScript object. Specifically, it's a JavaScript array. [JSON](http://json.org) is a textual data interchange notation. Source code is not JSON. – T.J. Crowder Sep 26 '13 at 16:41
  • 3
    Why is this closed? While the original post wasn't well formatted, the question is valid and the issue is now quite clear (JavaScript objects do not have an inherint `.length` property). – quietmint Sep 26 '13 at 17:11
  • 1
    Nominated for reopening. Google brought me here and my question was answered. –  Jul 26 '18 at 17:46
  • Actually upon further review, it should be closed as a duplicate of https://stackoverflow.com/questions/22580223/get-length-of-json-object/22580637 –  Jul 26 '18 at 17:48

2 Answers2

20

JavaScript doesn't have a .length property for objects. If you want to work it out, you have to manually iterate through the object.

function objLength(obj){
  var i=0;
  for (var x in obj){
    if(obj.hasOwnProperty(x)){
      i++;
    }
  } 
  return i;
}


alert(objLength(JSONObject)); //returns 4

Edit:

Javascript has moved on since this was originally written, IE8 is irrelevant enough that you should feel safe in using Object.keys(JSONObject).length instead. Much cleaner.

Doug
  • 3,312
  • 1
  • 24
  • 31
  • 2
    That would return only the length of _enumerable_ properties of the object that are _directly_ on it (excluding enumerables on the prototype chain). This can be written (in modern JS) as `Object.keys(obj).length` by the way. – Benjamin Gruenbaum Sep 26 '13 at 16:39
  • 1
    Or consider [`Object.keys`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) (enumerable properties) or [`Object.getOwnPropertyNames`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames) (all properties) – quietmint Sep 26 '13 at 16:39
  • That's incredibly neat Benjamin, I like it. It's worth noting though that it doesn't work in IE8, so currently unusable without shims. – Doug Sep 30 '13 at 16:13
6

The following is actually an array of JSON objects :

var JSONObject = [{ "name":"John Johnson", "street":"Oslo West 16", "age":33,
"phone":"555 1234567"}, {"name":"John Johnson", "street":"Oslo West 16", 
"age":33, "phone":"555 1234567" }];

So, in JavaScript length is a property of an array. And in your second case i.e.

var JSONObject = {"name":"John Johnson", "street":"Oslo West 16", "age":33, 
"phone":"555 1234567"};

the JSON object is not an array. So the length property is not available and will be undefined. So you can make it as an array as follows:

var JSONObject = [{"name":"John Johnson", "street":"Oslo West 16", "age":33, 
"phone":"555 1234567"}];

Or if you already have object say JSONObject. You can try following:

var JSONObject = {"name":"John Johnson", "street":"Oslo West 16", "age":33, 
"phone":"555 1234567"};
var jsonObjArray = []; // = new Array();
jsonObjArray.push(JSONObject);

And you do get length property.

CramerTV
  • 1,376
  • 1
  • 16
  • 29
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164