0

Is there any way to get the length from the following:

{
    "11": {
        "id": "456",
        "uuid": "b596362a-b5bb-4n94-8fd5-1e9fh6fd877b",
        "name": "Test",
        "description": "Test"
    },
    "22": {
        "id": "739",
        "uuid": "c4ccbddf-5177-482f-b5e8-cdd4f699e6b7",
        "name": "Test2",
        "description": "Test2"
    },
    "33": {
        "id": "737",
        "uuid": "m4ccbddv-5177-482f-b5e8-cdd4f699e6b7",
        "name": "Test3",
        "description": "Test3"
    }
}

The length should be 3. I tried to use JSON.stringify(data).length but this gives the length of the whole string.

My God
  • 21,961
  • 23
  • 93
  • 166
  • Please use the search :P – Felix Kling May 17 '16 at 15:36
  • Or are you literally asking how to get the number of properties from an object *encoded as JSON*, without decoding it into a native JS value? It appears you are mistakenly calling a JS object "JSON" (which is a common mistake unfortunately), but I could be wrong. In other words, is `data` an *object* or a *string containing JSON*? – Felix Kling May 17 '16 at 15:38
  • Its a JSON string coming by applying `JSON.stringify`. – My God May 17 '16 at 15:45
  • So, if `data` is a "JSON string", why are you calling `JSON.stringify` on it *again*? Since you accepted once of the answers it seems I am right in assuming you are actually working with an object. – Felix Kling May 17 '16 at 15:46
  • No, I am not calling again. I am just printing it in console using `JSON.stringify` to get data. The original one was `JSON.stringify($rootScope.data)` which gave the JSON string as I shown you here. I just said that I used `JSON.stringify($rootScope.data).length` to get the length of that. – My God May 17 '16 at 15:51
  • Then it seems you misunderstood my comment. I asked you what the value of `data` is, which is what you are passing to `JSON.stringify`: `JSON.stringify(data).length`. Anyways, thanks for the clarification. – Felix Kling May 17 '16 at 15:52

3 Answers3

1

In addition to above answer, apparently, modern browsers have an Object.keys function. In this case, you could do this:

Object.keys(jsonArray).length;

You can use following function

function getJsonItemLength(item) {
  if (typeof item !== undefined && varNotNull(item) && item) {
        if (Array.isArray(item)) {
            return item.length;
        } else if (typeof item === 'object' ) {
            return Object.keys(item).length;
        } else if (typeof item === 'string' ) {
            return item.length
        } else {
            return 0;
        }
  }
  return 0;
}
  • The other answers are also all suggesting `Object.keys`. How is yours different? Which "above answer" are you referring to? – Felix Kling May 17 '16 at 15:41
0

using Object.keys you get the list of keys (in an array) so you could:

Object.keys(obj).length
maioman
  • 15,071
  • 4
  • 30
  • 42
0

If you're asking about the length of an associative-array-like object, that's your answer: Length of a JavaScript object

Or to save you the click:

Object.keys(myObj).length;
Community
  • 1
  • 1
Piotr Ma'niak
  • 230
  • 3
  • 13