JSON Objects

A JSON object is a container that holds related data together in the form of key-value pairs. It is surrounded by curly braces { }. Think of a JSON object like a student's ID card — it has multiple fields (name, roll number, class, photo) all grouped under one card.

Objects are the most fundamental building block in JSON. Almost every real-world JSON data is structured using objects.

Basic Structure of a JSON Object

{
  "key1": value1,
  "key2": value2,
  "key3": value3
}

Rules to remember for JSON objects:

  • The object starts with { and ends with }
  • Each key must be a string in double quotes
  • A colon : separates the key and the value
  • Multiple key-value pairs are separated by commas
  • The last key-value pair does not end with a comma

Simple JSON Object Example

Below is a JSON object representing a book:

{
  "title": "The Alchemist",
  "author": "Paulo Coelho",
  "pages": 197,
  "isAvailable": true
}

Each line is a key-value pair. The keys are "title", "author", "pages", and "isAvailable". The values are a string, a string, a number, and a boolean respectively.

Accessing Values in a JSON Object

When JSON is used inside JavaScript, data from an object can be accessed using the dot notation or bracket notation.

Consider this JSON object stored in a JavaScript variable:

const book = {
  "title": "The Alchemist",
  "author": "Paulo Coelho",
  "pages": 197
};

Using dot notation:

console.log(book.title);   // Output: The Alchemist
console.log(book.pages);   // Output: 197

Using bracket notation:

console.log(book["author"]);  // Output: Paulo Coelho

Nested JSON Objects

A JSON object can contain another object as a value. This is called a nested object. It is very useful when the data is complex and has sub-categories.

{
  "employee": {
    "name": "Kiran Patel",
    "age": 30,
    "department": {
      "name": "Engineering",
      "location": "Hyderabad"
    }
  }
}

In this example, the "employee" key holds an object, and inside that object, "department" also holds another object. This creates a tree-like structure.

To access "location" in JavaScript:

console.log(data.employee.department.location);  // Output: Hyderabad

JSON Object with Multiple Data Types

A single JSON object can contain values of different data types — string, number, boolean, null, array, and object all together.

{
  "productName": "Wireless Headphones",
  "brand": "SoundMax",
  "price": 2999,
  "inStock": true,
  "discount": null,
  "ratings": [4, 5, 4, 3, 5],
  "specifications": {
    "color": "Black",
    "batteryLife": "20 hours",
    "wireless": true
  }
}

Modifying Values in a JSON Object (JavaScript)

Once a JSON object is parsed into a JavaScript object, values can be updated directly:

const product = {
  "productName": "Wireless Headphones",
  "price": 2999
};

product.price = 2499;
console.log(product.price);  // Output: 2499

Adding a New Key to a JSON Object (JavaScript)

New key-value pairs can be added to an existing JSON object after parsing:

product.color = "Blue";
console.log(product.color);  // Output: Blue

Deleting a Key from a JSON Object (JavaScript)

The delete keyword removes a key-value pair from an object:

delete product.color;
console.log(product.color);  // Output: undefined

Looping Through a JSON Object (JavaScript)

To go through all the keys and values of a JSON object, use a for...in loop:

const person = {
  "name": "Meena",
  "age": 28,
  "city": "Kolkata"
};

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

// Output:
// name: Meena
// age: 28
// city: Kolkata

Key Points to Remember

  • A JSON object is always wrapped in { }
  • Keys must be strings in double quotes
  • Values can be any valid JSON data type
  • Objects can be nested inside other objects
  • In JavaScript, use dot notation or bracket notation to access values
  • The last key-value pair should not have a trailing comma

Summary

A JSON object is the primary way to structure data in JSON. It groups related information under one roof using key-value pairs. Objects can contain values of any type — including other objects and arrays — making them powerful and flexible for representing real-world data of any complexity.

Leave a Comment

Your email address will not be published. Required fields are marked *