How to Iterate JSON Object in JavaScript?
Last Updated :
07 Jun, 2025
In JavaScript, there are different ways to iterate over the properties of a JSON object. Let’s look at the most common techniques.
1. Using for...in Loop
The for...in loop is a simple way to go through the properties of a JSON object. It loops over the keys of the object. Inside the loop, we can access each key's value using obj[key].
JavaScript
const obj = {
"company": 'GeeksforGeeks',
"contact": '+91-9876543210',
"city": 'Noida'
};
for (const key in obj) {
console.log(`${key}: ${obj[key]}`);
}
Outputcompany: GeeksforGeeks
contact: +91-9876543210
city: Noida
2. Using Object.keys() and Array forEach() Method
Object.keys() can be used to get an array of the keys from a JSON object. This array of keys can then be iterated over using the forEach method. Object.keys(obj) returns the keys of the JSON object. Inside the forEach loop, each property value can be accessed using obj[key]
JavaScript
const obj = {
"company": 'GeeksforGeeks',
"contact": '+91-9876543210',
"city": 'Noida'
};
Object.keys(obj).forEach(key => {
console.log(`${key}: ${obj[key]}`);
});
Outputcompany: GeeksforGeeks
contact: +91-9876543210
city: Noida
3. Using Object.entries() and Array forEach() Method
To access both the keys and values of a JSON object, Object.entries() can be used. This method returns an array of [key, value] pairs from the object. The forEach method can then be applied to iterate through the array of entries. Inside the loop, both the key and value of each property can be accessed directly.
JavaScript
const obj = {
"company": 'GeeksforGeeks',
"contact": '+91-9876543210',
"city": 'Noida'
};
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
Outputcompany: GeeksforGeeks
contact: +91-9876543210
city: Noida
4. Using for...of Loop with Object.entries() Method
The for...of loop can be used with Object.entries() to iterate over the [key, value] pairs of a JSON object. The Object.entries(obj) method returns an array of [key, value] pairs from the object. The for...of loop then iterates through this array, allowing direct access to both the key and value of each property.
JavaScript
const obj = {
"company": 'GeeksforGeeks',
"contact": '+91-9876543210',
"city": 'Noida'
};
for (const [key, value] of Object.entries(obj)) {
console.log(`${key}: ${value}`);
}
Outputcompany: GeeksforGeeks
contact: +91-9876543210
city: Noida
5. Using the Fetch API
When retrieving data from an external API or server, it's common to get the response in JSON format. The Fetch API is a modern method to make HTTP requests and retrieve this data.
JavaScript
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(data => {
for (const [key, value] of Object.entries(data[0])) {
console.log(key + ": " + value);
}
})
.catch(error => console.log('Error fetching data:', error));
Output
id: 1
name: Leanne Graham
username: Bret
email: Sincere@april.biz
address: {"street":"Kulas Light","suite":"Apt. 556","city":"Gwenborough","zipcode":"92998-3874","geo":{"lat":"-37.3159","lng":"81.1496"}}
phone: 1-770-736-8031 x56442
website: hildegard.org
company: {"name":"Romaguera-Crona","catchPhrase":"Multi-layered client-server neural-net","bs":"harness real-time e-markets"}
Conclusion
In JavaScript, there are various ways to iterate over a JSON object, such as using the for...in loop, Object.keys(), Object.entries(), and for...of. Each method provides a simple and effective way to access the keys and values of a JSON object, depending on the use case. The Fetch API also makes it easy to retrieve and work with JSON data from external sources.
Similar Reads
How to iterate over a JavaScript object ?
Iteration involves looping through the object's properties one by one. Depending on the method used, you can access and manipulate different levels of properties efficiently. Here are several methods.There are many methods to iterate over an object which are discussed below: Table of ContentUsing fo
3 min read
How to Check if Object is JSON in JavaScript ?
JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
2 min read
Converting JSON text to JavaScript Object
Pre-requisite: JavaScript JSON JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but itâs available for use by many languages including Python, Ruby, PHP, and Java and hence, it can be said as l
3 min read
How to Parse JSON in JavaScript ?
Parse JSON in JavaScript, accepting a JSON string as input and returning a corresponding JavaScript object with two methods, using JSON.parse() for parsing JSON strings directly and employing the fetch API to parse JSON responses from web APIs. These techniques are crucial for seamless data manipula
2 min read
Are Objects Iterable in JavaScript?
In JavaScript, plain objects are not iterable by default, meaning you cannot directly use constructs like for...of loops or the spread operator (...) with them. However, arrays (a), strings, maps, and sets are inherently iterable. Understanding Iterables in JavaScriptAn iterable is an object that im
2 min read
How to Master JSON in JavaScript?
JSON is a text format for representing structured data, typically in the form of key-value pairs. It primarily sends data between a server and a client, especially in web APIs.Objects are enclosed in curly braces {} and contain key-value pairs.Arrays are enclosed in square brackets [] and hold value
5 min read
JavaScript | Add new attribute to JSON object
The task is to add a JSON attribute to the JSON object. To do so, Here are a few of the most used techniques discussed.Example 1: This example adds a prop_11 attribute to the myObj object via var key. html <!DOCTYPE HTML> <html> <head> <title> JavaScript | Add new attribute t
2 min read
How to change JSON String into an Object in JavaScript ?
In this article we are going to learn how to change JSON String into an object in javascript, JSON stands for JavaScript object notation. It is the plain format used frequently as a communication medium on the internet. It appears close to OOP language like JavaScript but cannot be accessed like Jav
3 min read
How to iterate over Map elements in JavaScript ?
Map() is very useful in JavaScript it organises elements into key-value pairs. This method iterates over each element in an array and applies a callback function to it, allowing for modifications before returning the updated array." The Map() keyword is used to generate the object. The map() method
3 min read
How to Convert JSON Object to CSV in JavaScript ?
JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used formats, each with its own strengths and applications. Fortunately, JavaScript provides powerful tools to facilitate the conversion process between these formats. These are the following approaches: Table of Conte
3 min read