Computer Programming 1 (JavaScript) • Data I/O & JSON

Working with Nested JSON — I can read and update nested values in a JSON object at my level.

Learn to navigate and modify data structures that live inside other data structures — a must-have skill for modern web development.

A JavaScript data file with code brackets and a laptop.
Inside this lesson 10 sections 5 questions 5 figures

SECTION 1

Historical Context & Motivation

Before the internet became the rich, interactive experience we know today, web pages were mostly static documents. Servers sent HTML files, and that was that. But as websites grew more complex — think social media feeds, weather apps, and online stores — developers needed a lightweight way to send structured data between servers and browsers. The solution was JSON, which stands for JavaScript Object Notation. JSON borrowed its syntax directly from JavaScript objects, making it natural for web developers to read and write.

1999 • JSON Is Born

Douglas Crockford begins promoting a lightweight data-interchange format based on JavaScript's object literal syntax. He names it JSON.

2005 • AJAX Revolution

The rise of AJAX (Asynchronous JavaScript and XML) makes JSON the preferred format for sending data between browsers and servers, replacing heavier XML in many projects.

2013 • ECMA Standard

JSON is formally standardized as ECMA-404, cementing its role as the universal data format on the web. REST APIs almost universally return JSON.

2020s • Deeply Nested Data Everywhere

Modern APIs — from social media to gaming leaderboards — routinely return nested JSON objects several layers deep, making the ability to read and update nested values an essential skill.

Real-world data is rarely flat. A user profile doesn't just have a name and an age — it has an address (with a street, city, and zip code), a list of friends, and settings with sub-categories. That means the JSON representing this data contains objects inside objects and arrays inside objects. The central question of this lesson is: how do you reach into those inner layers to read a value or change it?

SECTION 2

Core Principles & Definitions

Before diving into nested structures, let's make sure the foundational vocabulary is solid. A JSON object is a collection of key-value pairs wrapped in curly braces { }. A key is always a string, and a value can be a string, number, boolean, null, array, or another object. When one of those values is itself an object or an array, we call the structure nested.

1

Dot Notation

Access a property using a period: user.name. Clean and readable, but the key must be a valid identifier (no spaces or special characters).
2

Bracket Notation

Access a property using square brackets: user["name"]. Required when keys have spaces, start with numbers, or are stored in variables.
3

Chaining Access

To reach deeper layers, chain notations together: user.address.city. Each dot or bracket moves you one level deeper into the nested structure.
4

Updating Values

Assign a new value to a nested property using =: user.address.city = "Austin". The original object is mutated in place.
5

Arrays in JSON

Arrays use zero-based indexes: user.hobbies[0] returns the first hobby. You can mix dot and bracket notation freely when arrays are nested inside objects.
✦

KEY TAKEAWAY

Think of a nested JSON object like a set of Russian nesting dolls (matryoshka). The outermost doll is the top-level object. To get to a smaller doll inside, you have to open each layer one at a time. In code, each dot or bracket is like opening the next doll. If you want to repaint the smallest doll, you still have to open every doll above it first — then you can make your change.

SECTION 3

Visualizing Nested JSON Structure

The diagram below shows a sample JSON object representing a student record. Notice how the top-level object contains simple values (like name and grade), but also contains an inner object for address and an inner array for courses. Each colored box represents one nesting level.

student — Top-Level JSON Object{ }"name": "Mia Chen""grade": 11"active": true"address":{ } — nested object"street": "42 Oak Lane""city": "Portland""zip": "97201""courses":[ ] — array of objectsindex 0"title": "Biology", "period": 2index 1"title": "JS Intro", "period": 5index 2"title": "History", "period": 7
The blue dashed border is the top-level object. The violet box is a nested object (address). The pink box is a nested array (courses), and each colored card inside it is an object at a specific array index.

To read the city from the diagram above, you would write student.address.city, which returns "Portland". To read the title of the second course, you would write student.courses[1].title, which returns "JS Intro". Each dot or bracket in the chain moves you one layer deeper, matching the boxes in the diagram.

SECTION 4

How Dot and Bracket Notation Work

JavaScript gives you two tools for accessing properties: dot notation and bracket notation. They achieve the same result, but each has situations where it shines. Understanding both — and when to use which — is key to working with nested JSON confidently.

Dot Notation

Dot notation is the most common way to access properties. You type the object name, a period, and the property name: student.name. For nested access, you chain dots: student.address.city. The key restriction is that the property name must be a valid JavaScript identifier — it cannot start with a number, contain spaces, or include hyphens.

Bracket Notation

Bracket notation uses square brackets with a string inside: student["name"]. This is required when the key has special characters, like student["home-address"]. It is also essential when the key is stored in a variable. For example, if let key = "city"; then student.address[key] dynamically looks up "city" at runtime.

Reading vs. Updating

Reading and updating use the same path syntax. The only difference is the assignment operator. To read: let city = student.address.city; — this copies the value into the variable city. To update: student.address.city = "Seattle"; — this changes the value stored in the original object. The path to the left of = tells JavaScript exactly which nested property to modify.

⚠

Watch Out: undefined Errors

If you try to access a property that doesn't exist, JavaScript returns undefined. If you then try to go one level deeper on undefined — for example, student.phone.areaCode when there is no phone property — you'll get a TypeError. Always confirm that intermediate objects exist before reaching deeper.

SECTION 5

Common Nesting Patterns

Nested JSON appears in predictable patterns. Once you recognize these patterns, navigating any API response or configuration file becomes much easier. The diagram below illustrates the three most common nesting patterns you'll encounter.

Three Common Nesting PatternsObject in Object{"user": {"profile": {"bio": "Hi!"}}}Access path:data.user.profile.bioSettings, configs, profilesArray in Object{"team": "Hawks","scores": [88, 92, 76]}Access path:data.scores[1]Lists, tags, historyObject in Array{"items": [{ "id": 1,"name": "Pen" },{ "id": 2,"name": "Book" }]}Access path:data.items[0].nameAPI results, records
Three patterns side by side. Object in Object chains dots. Array in Object mixes dots with bracket indexes. Object in Array uses a bracket index followed by a dot to reach a property on an array element.
PatternExample PathWhen You See It
Object → Objectobj.inner.propSettings pages, user profiles, configuration files
Object → Arrayobj.list[i]Lists of scores, tags, comments, or log entries
Array → Objectobj.list[i].propAPI search results, rows from a database, shopping cart items
Mixed / Multi-levelobj.a[i].b.c[j]Complex API responses, game save data, social media feeds

Common nesting patterns and where they appear in real projects.

SECTION 6

Worked Example — Reading & Updating a Game Profile

Let's work through a realistic example. Imagine you are building a small game and you store player data in a JSON object. Here is the starting data:

let player = { "username": "SkyRider99", "level": 12, "stats": { "health": 85, "mana": 40, "inventory": [ { "item": "sword", "damage": 15 }, { "item": "shield", "defense": 10 }, { "item": "potion", "heal": 25 } ] }, "settings": { "sound": true, "difficulty": "hard" } };

Task: Read the player's health, then update their difficulty to "easy", and finally read the damage of the first inventory item.

  1. 1

    Step 1 — Read Health

    Health lives inside the stats object, which is inside the top-level player object. So the path is: player.stats.health.
    Returns: 85
  2. 2

    Step 2 — Update Difficulty

    The difficulty property is inside the settings object. We use the same path, but now we assign a new value: player.settings.difficulty = "easy";. After this line runs, player.settings.difficulty holds "easy" instead of "hard".
    player.settings.difficulty is now "easy"
  3. 3

    Step 3 — Read Damage of First Inventory Item

    The inventory is an array inside stats. The first item is at index 0. Each item is an object, so we chain into its damage property: player.stats.inventory[0].damage.
    Returns: 15
  4. 4

    Step 4 — Verify the Full Path Breakdown

    Let's trace player.stats.inventory[0].damage piece by piece. player → the whole object. .stats → the nested stats object. .inventory → the array inside stats. [0] → first element of that array (an object). .damage → the damage property of that element.
    Each segment of the path opens the next layer, just like the nesting-doll analogy.

SECTION 7

Dot Notation vs. Bracket Notation — When to Use Which

Both notations let you access nested values, but they differ in flexibility. The table below summarizes the trade-offs so you can pick the right tool for each situation.

FeatureDot NotationBracket Notation
Syntaxobj.keyobj["key"]
ReadabilityVery clean and easy to scanSlightly more verbose
Dynamic keys (variables)❌ Not supported✅ Supported — use a variable inside brackets
Keys with spaces / hyphens❌ Syntax error✅ Works fine
Array indexes❌ Must use brackets✅ Required for array access
Best forMost everyday property accessDynamic lookups, unusual key names, arrays

Side-by-side comparison of JavaScript's two property access methods.

✦

KEY TAKEAWAY

Think of dot notation as using the speed dial on your phone — quick and convenient when you know the exact contact name. Bracket notation is like opening the full contacts list and searching — a little more effort, but you can look up anyone, even if their name has special characters. In practice, you'll use dot notation most of the time and reach for brackets when the situation demands it.

SECTION 8

Connection to Advanced Concepts

Reading and updating nested JSON is the foundation for many advanced techniques you'll encounter as you continue programming. The skills you're building now scale directly to handling API responses, managing application state, and working with databases.

What You Learn NowWhere It Leads
Accessing nested properties with dot/bracket notationDestructuring assignment in ES6 — a shortcut for pulling out nested values
Updating nested values with the = operatorImmutable state updates in frameworks like React, where you create new objects instead of mutating
Recognizing nesting patternsParsing complex API responses from services like weather or social media APIs
Handling undefined with careful path checksOptional chaining (?.) in modern JavaScript, which prevents TypeErrors automatically

How today's lesson connects to what comes next.

One particularly exciting extension is optional chaining (?.), introduced in ES2020. Instead of writing lengthy if checks to avoid TypeErrors, you can write player?.stats?.inventory?.[0]?.damage. If any link in that chain is undefined or null, the whole expression short-circuits to undefined instead of crashing. But to appreciate that shortcut, you first need to understand the manual path-building you've learned in this lesson.

SECTION 9

Practice Problems

Use the following JSON object for all five problems:

let school = { "name": "Lincoln High", "address": { "street": "500 Elm St", "city": "Denver", "state": "CO" }, "clubs": [ { "name": "Robotics", "members": 18, "advisor": "Ms. Park" }, { "name": "Drama", "members": 25, "advisor": "Mr. Reyes" }, { "name": "Coding", "members": 12, "advisor": "Dr. Amin" } ], "principal": { "name": "Dr. Johnson", "contact": { "email": "djohnson@lincoln.edu", "phone": "303-555-0199" } } };

Question 1: CONCEPTUAL

Explain in your own words why school.address returns an object rather than a single value like a string or number. What does this tell you about how JSON represents complex, real-world information?
Show answer
The key "address" has an object as its value because a real-world address is made up of multiple pieces — street, city, and state. JSON mirrors this by allowing an object (with its own key-value pairs) to be the value of a property. This nesting lets developers represent grouped, related data instead of flattening everything into separate top-level keys, which would make the structure harder to read and maintain.

Question 2: BASIC

Write the JavaScript expression that reads the principal's email address.
Show answer
The path is school.principal.contact.email. Starting from school, we go into principal (an object), then into contact (another nested object), and finally to email. This returns the string "djohnson@lincoln.edu".

Question 3: INTERMEDIATE

Write two lines of JavaScript: one that reads the name of the second club, and another that updates the number of members in the Coding club to 15.
Show answer
To read the second club's name: let clubName = school.clubs[1].name; — this returns "Drama" because arrays are zero-indexed, so index 1 is the second element. To update the Coding club's members: school.clubs[2].members = 15; — the Coding club is at index 2, and assigning a new value to .members changes it from 12 to 15 in the original object.

Question 4: APPLIED

You want to build a feature that displays a sentence like: "The Robotics club at Lincoln High is advised by Ms. Park." Write JavaScript code that constructs this sentence using only values accessed from the school object (do not hard-code any of the names).
Show answer
One solution uses template literals: let sentence = `The ${school.clubs[0].name} club at ${school.name} is advised by ${school.clubs[0].advisor}.`; Here, school.clubs[0].name gives "Robotics", school.name gives "Lincoln High", and school.clubs[0].advisor gives "Ms. Park". By reading from the object instead of hard-coding, the sentence automatically updates if the data changes.

Question 5: CRITICAL THINKING

Suppose you receive the club name as a user input stored in a variable: let target = "Drama"; You don't know which index that club is at. Write a short block of code that finds the matching club object and updates its advisor to "Mrs. Lee". Explain why you cannot simply use school.clubs[target].
Show answer
You cannot write school.clubs[target] because clubs is an array, and arrays use numeric indexes — "Drama" is not a number. Instead, you need to search the array. A common approach: for (let i = 0; i < school.clubs.length; i++) { if (school.clubs[i].name === target) { school.clubs[i].advisor = "Mrs. Lee"; break; } } This loop checks each element's name property and updates the advisor when it finds a match. An alternative is school.clubs.find(c => c.name === target).advisor = "Mrs. Lee"; using the array find() method, which is more concise but requires understanding callback functions.

SUMMARY

Lesson Summary

Summary

Nested JSON is how real-world data — user profiles, API responses, game save files — is organized in modern web development. A JSON object stores key-value pairs, and when a value is itself an object or an array, the structure becomes nested. You navigate these layers using dot notation for simple keys and bracket notation for dynamic keys, special characters, and array indexes. Chaining these notations together — like obj.list[0].prop — lets you reach any depth.

To read a nested value, write the full path as an expression. To update a nested value, place the same path on the left side of the assignment operator =. Remember to watch for undefined at intermediate levels — trying to access a property on undefined throws a TypeError. Mastering these skills prepares you for API consumption, destructuring, and optional chaining in more advanced JavaScript courses.

Varsity Tutors • Computer Programming 1 (JavaScript) • Working with Nested JSON