To get the number of keys in a JSON object in javascript you can use one of the following 2 methods.
Using Object.keys()
The Object.keys() method returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop.
Example
let a = {
name: "John",
age: 32,
city: "Hong Kong"
}
console.log(Object.keys(a).length)Output
This will give the output −
3
Using for in loop
The for...in statement iterates over all non-Symbol, enumerable properties of an object. For example,
Example
let a = {
name: "John",
age: 32,
city: "Hong Kong"
}
let count = 0;
for(let key in a) {
count ++;
}
console.log(count)Output
This will give the output −
3