# JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format commonly used for representing structured data. Iterating over JSON data is a fundamental operation in JavaScript programming. In this guide, we will explore various methods to iterate over JSON data using JavaScript.

### Table of Contents

1. Introduction to JSON
2. Iterating over JSON
   * Using for...in Loop
   * Using Object.keys()
   * Using Object.values()
   * Using Object.entries()
   * Using forEach()
   * Using for...of Loop
3. Conclusion

### Introduction to JSON

JSON is a text-based data format that is easy for humans to read and write and easy for machines to parse and generate. It is commonly used for exchanging data between a server and a web application.

A JSON object is a collection of key-value pairs where keys are strings, and values can be any valid JSON data type, such as strings, numbers, arrays, or other objects.

```json
{
  "name": "John Doe",
  "age": 30,
  "city": "New York",
  "emails": ["john@example.com", "doe@example.com"]
}
```

### Iterating over JSON

#### Using for...in Loop

The `for...in` loop iterates over the enumerable properties of an object, including properties inherited from its prototype chain.

```javascript
const person = {
  name: "John Doe",
  age: 30,
  city: "New York"
};

for (let key in person) {
  console.log(`${key}: ${person[key]}`);
}
```

#### Using Object.keys()

The `Object.keys()` method returns an array of a given object's own enumerable property names.

```javascript
const person = {
  name: "John Doe",
  age: 30,
  city: "New York"
};

Object.keys(person).forEach(key => {
  console.log(`${key}: ${person[key]}`);
});
```

#### Using Object.values()

The `Object.values()` method returns an array of a given object's own enumerable property values.

```javascript
const person = {
  name: "John Doe",
  age: 30,
  city: "New York"
};

Object.values(person).forEach(value => {
  console.log(value);
});
```

#### Using Object.entries()

The `Object.entries()` method returns an array of a given object's own enumerable string-keyed property `[key, value]` pairs.

```javascript
const person = {
  name: "John Doe",
  age: 30,
  city: "New York"
};

Object.entries(person).forEach(([key, value]) => {
  console.log(`${key}: ${value}`);
});
```

#### Using forEach()

Arrays in JavaScript have a built-in `forEach()` method that can be used to iterate over array elements.

```javascript
const emails = ["john@example.com", "doe@example.com"];

emails.forEach(email => {
  console.log(email);
});
```

#### Using for...of Loop

The `for...of` loop is used to iterate over iterable objects, including arrays.

```javascript
const emails = ["john@example.com", "doe@example.com"];

for (let email of emails) {
  console.log(email);
}
```

### Conclusion

Iterating over JSON data is a common task in JavaScript programming. In this guide, we explored various methods for iterating over JSON objects and arrays, including `for...in` loop, `Object.keys()`, `Object.values()`, `Object.entries()`, `forEach()` method, and `for...of` loop. Each method has its own use case and can be chosen based on the specific requirements of the application. Choose the method that best fits your use case to efficiently iterate over JSON data in JavaScript.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guvi.gitbook.io/fsd/docs/module-1-js-basics/json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
