

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the size of a json object in JavaScript?
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
- Related Questions & Answers
- Explain how to get the size of a JSON array response in Rest Assured.
- How to convert JSON text to JavaScript JSON object?
- How to get the size of an array within a nested JSON in Rest Assured?
- How to deserialize a JSON into Javascript object?
- How to get all the keys of a JSON object using GSON in Java?
- How to only get the data of the nested JSON object in MongoDB?
- How to parse JSON object in JavaScript?
- How to get the values of the different types from a JSON object in Java?
- Sorting a JSON object in JavaScript
- Flattening a JSON object in JavaScript
- How to construct a JSON object from a subset of another JSON object in Java?
- How to turn a JSON object into a JavaScript array in JavaScript ?
- How to transform JSON text into a JavaScript object?
- How to modify an array value of a JSON object in JavaScript?
- How to convert a JSON string into a JavaScript object?
Advertisements