0% found this document useful (0 votes)
2 views7 pages

JSON Practice JavaScript

The document provides an overview of JSON (JavaScript Object Notation), highlighting its lightweight nature and ease of use for both humans and machines. It includes practice examples demonstrating how to create JSON objects, access values, work with JSON arrays, convert JSON to strings, and parse JSON strings back into objects. Key concepts include key-value pairs and arrays as the foundational structures of JSON.

Uploaded by

jadhao060704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

JSON Practice JavaScript

The document provides an overview of JSON (JavaScript Object Notation), highlighting its lightweight nature and ease of use for both humans and machines. It includes practice examples demonstrating how to create JSON objects, access values, work with JSON arrays, convert JSON to strings, and parse JSON strings back into objects. Key concepts include key-value pairs and arrays as the foundational structures of JSON.

Uploaded by

jadhao060704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JSON in JavaScript

Creation, Key-Value Pairs, Access,


Arrays, and Practice Examples
JSON Recap
• • JSON (JavaScript Object Notation) is a
lightweight data-interchange format.

• • It is easy to read and write for humans and


easy to parse and generate for machines.

• • JSON is built on two structures: key-value


pairs and arrays.
Practice Example 1: Create a JSON
Object
• Task: Create a JSON object representing a book with title, author, and year.
• Solution:
• const book = {
"title": "The Alchemist",
"author": "Paulo Coelho",
"year": 1988
};

console.log(book);
Practice Example 2: Access JSON
Values
• Task: Access the author's name from the book object.
• Solution:
• console.log(book.author); // Output: Paulo Coelho
Practice Example 3: JSON Array of
Objects
• Task: Create a JSON array of two users with name and email.
• Solution:
• const users = [
{ "name": "Alice", "email": "[email protected]" },
{ "name": "Bob", "email": "[email protected]" }
];

console.log(users[1].email); // Output: [email protected]


Practice Example 4: Convert JSON
to String
• Task: Convert the book object to a JSON string.
• Solution:
• const jsonString = JSON.stringify(book);
console.log(jsonString);
Practice Example 5: Parse JSON
String
• Task: Parse the jsonString back into a JavaScript object.
• Solution:
• const parsedBook = JSON.parse(jsonString);
console.log(parsedBook.title); // Output: The Alchemist

You might also like