- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
In this article, we will learn about various methods to get the size of a JSON object in JavaScript.
Getting the size of the object in JavaScript is same as the counting the total number of keys of an object. In general, we can get the size or number of keys of a JSON object in JavaScript by using two methods.
Using object.keys()
The object.keys() method returns an array of the given object of its countable properties name with the same order as we get as a normal loop. Following is the syntax of this method −
Object.keys(name_of_object).length;
Example
Following is an example to get the size of a JSON object in JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <title>Size of json</title> </head> <body> <script> var myObject = { name: "tutorialspoint", address: "above D-mart", city: "hyderabad", }; var size = Object.keys(myObject).length; document.write(size); </script> </body> </html>
Using the for…in loop
The for…in loop iterates over all non-symbols, enumerable properties of an object. Following is the syntax of the for…in loop −
for (let key in myObject){ … }:
In the above for loop, the key is the property of the object and myObject is the name of the object.
Example
In here we are trying to print the size of a JSON object using the for…in loop.
<!DOCTYPE html> <html lang="en"> <head> <title>Size of json</title> </head> <body> <script> let myObject = { name: "tutorialspoint", address: "above D-mart", city: "hyderabad", }; let count = 0; //declaring count as 0 for (let key in myObject) { count++; } document.write(count); </script> </body> </html>
Using for loop
We can also get the number of elements in a JSON object using the for loop to do so, you need to define a variable and loop through the object(if JSON assigns it to the JS object) and increment a variable on each loop execution. Once the loops have read all the elements of your object you can print the size.
Example
Let us see an example for this −
<!DOCTYPE html> <html lang="en"> <head> <title>Size of json</title> </head> <body> <script> Object.size = function (obj) { var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) size++; } return size; }; var obj = { name: "tutorialspoint", address: "above D-mart", city: "hyderabad", }; var size = Object.size(obj); document.write("The Object has a length of " + size); </script> </body> </html>
- Related Articles
- 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 parse JSON object in JavaScript?
- 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 turn a JSON object into a JavaScript array in JavaScript ?
- How to only get the data of the nested JSON object in MongoDB?
- How to get the values of the different types from a JSON object in Java?
- How to modify an array value of a JSON object in JavaScript?
- How to transform JSON text into a JavaScript object?
- Get value for key from nested JSON object in JavaScript
- Sorting a JSON object in JavaScript
- Flattening a JSON object in JavaScript
- How to convert a JSON string into a JavaScript object?
