Nested JSON Objects - potatoscript/json GitHub Wiki

🎩 Nested JSON Objects: Unlocking the Layers!

Welcome to the magical world of nested JSON objects! 🌟✨
In this adventure, we’ll explore how to put objects inside other objects and how to access them like a pro.
Even a primary school student will master nested objects after this! πŸš€


πŸ“¦ What is a Nested JSON Object?

A nested JSON object is like a box inside another box. 🧰🎁
Just imagine a magic treasure chest where each compartment holds another tiny chest.
In JSON, when an object contains another object as its value, it’s called a nested object.


🎯 Basic Example: Nested Object

{
  "person": {
    "name": "Lucy",
    "age": 25,
    "address": {
      "city": "Tokyo",
      "country": "Japan"
    }
  }
}

πŸ”Ž Explanation:

  • person is the main object.
  • Inside person, we have name, age, and another object called address.
  • The address object contains two key-value pairs: city and country.

πŸŽ‰ Story Time: The Magic Castle!

Imagine you’re exploring a magic castle. 🏰✨

  • The castle is the main object (person).
  • Inside the castle, there’s a room called address where the city and country are kept.

Let’s unlock the secrets of the castle! πŸ”


πŸ•΅οΈ Accessing Nested JSON Objects

To access the values inside a nested object, use the dot notation or bracket notation.


🎯 Example: Accessing Nested Values

{
  "person": {
    "name": "Lucy",
    "age": 25,
    "address": {
      "city": "Tokyo",
      "country": "Japan"
    }
  }
}

πŸ’‘ Dot Notation (JavaScript Example)

// Accessing nested values
console.log(person.name);             // Lucy
console.log(person.address.city);     // Tokyo
console.log(person.address.country);  // Japan

πŸ”Ž Bracket Notation (JavaScript Example)

// Accessing with bracket notation
console.log(person["name"]);                 // Lucy
console.log(person["address"]["city"]);      // Tokyo
console.log(person["address"]["country"]);   // Japan

πŸ•°οΈ Nested JSON with Multiple Layers

You can nest as many objects as you want! It’s like a treasure chest inside another treasure chest! πŸŽπŸ”‘


🧠 Example: Deeply Nested Object

{
  "university": {
    "name": "Tokyo University",
    "location": {
      "city": "Tokyo",
      "country": "Japan"
    },
    "departments": {
      "science": {
        "professor": "Dr. Tanaka",
        "students": 200
      },
      "arts": {
        "professor": "Ms. Sato",
        "students": 150
      }
    }
  }
}

πŸ”Ž Explanation:

  • university is the main object.
  • location is a nested object inside university.
  • departments is another object with two nested objects: science and arts.

🎯 Accessing Deeply Nested Objects

πŸ” Dot Notation Example

console.log(university.name);                    // Tokyo University
console.log(university.location.city);           // Tokyo
console.log(university.departments.science.professor);  // Dr. Tanaka

πŸ•΅οΈ Bracket Notation Example

console.log(university["name"]);                          // Tokyo University
console.log(university["location"]["city"]);              // Tokyo
console.log(university["departments"]["arts"]["students"]); // 150

⚑ Modifying Nested JSON Objects

You can also update or change values inside nested objects. πŸ“


πŸ“š Example: Updating a Nested Value

// Change the professor in the science department
university.departments.science.professor = "Dr. Yamada";

console.log(university.departments.science.professor);  // Dr. Yamada

πŸ› οΈ Adding New Nested Properties

You can also add new properties inside a nested object! 🌟


🌱 Example: Adding a New Key

// Add a new key to the address object
person.address.postalCode = "123-4567";

console.log(person.address.postalCode);  // 123-4567

πŸ•°οΈ Looping Through Nested Objects

When dealing with deeply nested objects, you might need to loop through them.
Use a for...in loop to explore the keys and values. πŸ”„


πŸ”„ Example: Looping Through Nested Object

for (let key in university.departments) {
  console.log(key + ": " + university.departments[key].professor);
}

🎯 Output:

science: Dr. Tanaka
arts: Ms. Sato

🎯 Converting Nested JSON to String

If you want to convert a nested JSON object into a string, use JSON.stringify().


πŸ“š Example: Stringify a Nested Object

let jsonString = JSON.stringify(person);
console.log(jsonString);

πŸŽ‰ Output:

{"name":"Lucy","age":25,"address":{"city":"Tokyo","country":"Japan","postalCode":"123-4567"}}

🧠 Converting JSON String to Nested Object

To convert a JSON string back to a nested object, use JSON.parse().


πŸ§ͺ Example: Parse a JSON String

let parsedObject = JSON.parse(jsonString);
console.log(parsedObject.address.city);  // Tokyo

⚑ Handling Deeply Nested JSON with Ease!

Dealing with deeply nested objects? Use conditional checks and loops to access values safely. 🎯


⚠️ Check if Nested Key Exists

if (person.address && person.address.city) {
  console.log(person.address.city);  // Tokyo
} else {
  console.log("City not found!");
}