3

Below is my JSON. I want to get number and name of array(s) in this object. This is dynamically created so I don't know about its number and name(s). Here are 2 Arrays in this example, named Table and Table1.

"{
      "Table": [
        {
          "Day": "Jan",
          "Counts": 20,
          "SrNo": 1,
          "Title": "test2",
          "ProfilePic": "/Image1.jpg" 
        },
        {
          "Day": "Feb",
          "Counts": 10,
          "SrNo": 2,        
          "Title": "test2",
          "ProfilePic": "/Image1.jpg"          
        }
    ],
 "Table1": [
    {
      "Day": "01",
      "Counts": 5,
      "SrNo": 1,       
      "Title": "test3",
      "ProfilePic": "/Image2.jpg"
     },
     {
      "Day": "02",
      "Counts": 9,
      "SrNo": 2,        
      "Title": "test3",
      "ProfilePic": "/Image2.jpg",        
     }
   ]
 }"
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Sunil Chaudhary
  • 397
  • 1
  • 9
  • 31

4 Answers4

10

Try below mentioned code,

Object.keys(jsonObject).length;

Also refer... : Get total number of items on Json object?

To get all the names :

var keys = Object.keys(jsonObject); // this will return root level title ["Table" , "Table1"]
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
Binary Brackets
  • 494
  • 1
  • 4
  • 12
4

Assuming that every property in the object contains an array, you can just count the number of properties using Object.keys, like this:

var arrayCount = Object.keys(obj).length;

Alternatively, if you actually want to determine the type of the property, in case there some other types in the object, you would need to loop through and check each property individually, which could be done using filter() like this:

var obj = {
  "Table": [{
      "Day": "Jan",
      "Counts": 20,
      "SrNo": 1,
      "Title": "test2",
      "ProfilePic": "/Image1.jpg"
    },
    {
      "Day": "Feb",
      "Counts": 10,
      "SrNo": 2,
      "Title": "test2",
      "ProfilePic": "/Image1.jpg"
    }
  ],
  "Table1": [{
      "Day": "01",
      "Counts": 5,
      "SrNo": 1,
      "Title": "test3",
      "ProfilePic": "/Image2.jpg"
    },
    {
      "Day": "02",
      "Counts": 9,
      "SrNo": 2,
      "Title": "test3",
      "ProfilePic": "/Image2.jpg",
    }
  ],
  'NotArray1': 'foo', // < not an array
  'isArray': false // < not an array
}

var arrayCount = Object.keys(obj).filter(function(key) {
  return obj[key].constructor === Array;
}).length;
console.log(arrayCount);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You can use Array.prototype.reduce() in order to return the total of all the object property values that are valid array:

var obj = {
    "Table": [{
      "Day": "Jan",
      "Counts": 20,
      "SrNo": 1,
      "Title": "test2",
      "ProfilePic": "/Image1.jpg"
    }, {
      "Day": "Feb",
      "Counts": 10,
      "SrNo": 2,
      "Title": "test2",
      "ProfilePic": "/Image1.jpg"
    }],
    "Table1": [{
        "Day": "01",
        "Counts": 5,
        "SrNo": 1,
        "Title": "test3",
        "ProfilePic": "/Image2.jpg"
      }, {
        "Day": "02",
        "Counts": 9,
        "SrNo": 2,
        "Title": "test3",
        "ProfilePic": "/Image2.jpg"
      }
    ],
    "Table2": false
  },
  arrayCount = Object.keys(obj).reduce(function (acc, val) {
    return Array.isArray(obj[val]) ? ++acc : acc;
  }, 0);

console.log(arrayCount);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

In a modern browser, to count arrays in a object

Object.keys(obj).filter((e) => Array.isArray(obj[e])).length
varatharajan
  • 229
  • 2
  • 15