I have JSON which looks like this:
{
"item1": {
"category": "fruit",
},
"item2": {
"category": "fruit",
}
}
How can I count the no. of items inside this?
I have JSON which looks like this:
{
"item1": {
"category": "fruit",
},
"item2": {
"category": "fruit",
}
}
How can I count the no. of items inside this?
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;