Objects & JSON

Learn about JavaScript objects, JSON data format, and object methods.

Present State

Objects in JavaScript allow for the storage of keyed collections of various data and more complex entities. JSON (JavaScript Object Notation) is a lightweight data-interchange format based on JavaScript object syntax, widely used for APIs and data exchange.

Code Example

Example demonstrating object creation, manipulation, and JSON usage:

JavaScript Object and JSON Example

// Create a JavaScript object
const user = {
  name: "Alice",
  age: 25,
  isAdmin: true,
  greet: function() {
    return `Hello, my name is ${this.name}`;
  }
};

// Accessing properties and calling methods
console.log(user.name);       // "Alice"
console.log(user.greet());    // "Hello, my name is Alice"

// Convert object to JSON
const jsonString = JSON.stringify(user);
console.log(jsonString);

// Convert JSON back to object
const parsedUser = JSON.parse(jsonString);
console.log(parsedUser.name); // "Alice"
          

This code demonstrates:

  • Creating and accessing object properties.
  • Using methods within objects.
  • Serializing objects to JSON using JSON.stringify().
  • Parsing JSON strings to objects with JSON.parse().

Description

JavaScript Objects: Collections of key-value pairs, where keys are strings and values can be any data type. Objects are used to represent real-world entities in code.

JSON: A data format derived from JavaScript object syntax, used for storing and exchanging data between servers and web applications.

Key Object Features:

  • Object.keys(obj) - Returns an array of object keys.
  • Object.values(obj) - Returns an array of values.
  • Object.entries(obj) - Returns an array of key-value pairs.
  • Methods can be defined directly within objects.
  • JSON.stringify(obj) - Converts an object to a JSON string.
  • JSON.parse(jsonStr) - Converts a JSON string to an object.

Applications

  • Modeling real-world data such as users, products, or settings.
  • Communicating with APIs using JSON payloads.
  • Storing configuration or state in structured form.
  • Representing structured data in client-side applications.
  • Data serialization and persistence in local storage or databases.

Multiple Choice Questions

Q1: Which method converts a JavaScript object to a JSON string?

  • A. JSON.parse()
  • B. JSON.convert()
  • C. JSON.stringify()
  • D. Object.toJSON()

Answer: C. JSON.stringify()

Q2: How do you access the value of a property name in an object user?

  • A. user.name
  • B. user->name
  • C. user(name)
  • D. user::name

Answer: A. user.name

Q3: What does JSON.parse() do?

  • A. Converts an object to a string.
  • B. Parses a JSON string to a JavaScript object.
  • C. Encrypts the JSON data.
  • D. Copies the object.

Answer: B. Parses a JSON string to a JavaScript object.

Q4: Which method retrieves all the keys of an object?

  • A. Object.values()
  • B. Object.entries()
  • C. Object.keys()
  • D. Object.getKeys()

Answer: C. Object.keys()