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:
personis the main object.- Inside
person, we havename,age, and another object calledaddress. - The
addressobject contains two key-value pairs:cityandcountry.
π 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
addresswhere 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:
universityis the main object.locationis a nested object insideuniversity.departmentsis another object with two nested objects:scienceandarts.
π― 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!");
}