-1

I have JSON which looks like this:

{
  "item1": {
    "category": "fruit",

  },
 "item2": {
    "category": "fruit",

  }
}

How can I count the no. of items inside this?

mistletoe
  • 463
  • 4
  • 11
  • 29

1 Answers1

1

As in your JSON, number of items are the number of keys. Thus, get your JSON in an variable (say obj) and use the following:

var  obj = JSON.parse('{"item1":{"category":"fruit",},"item2":{"category":"fruit",}}');

Object.keys(obj).length;
S Jayesh
  • 191
  • 1
  • 4
  • 19