In this lesson, you will understand JSON and how to convert data.
JSON stands for JavaScript Object Notation. It is a format to store and transport data. Think of it like a suitcase that holds your clothes. You pack your items in a structured way so you can find them easily. JSON does the same for data.
Imagine you want to keep track of your favorite fruits. Using JSON, you can organize this data like this:
{
"fruits": [
{ "name": "Apple", "color": "Red" },
{ "name": "Banana", "color": "Yellow" }
]
}
Here, each fruit has a name and a color, which is easy to read and use later.
File: convert_data.js
// Example of converting a JavaScript object to JSON
let fruits = [
{ name: "Apple", color: "Red" },
{ name: "Banana", color: "Yellow" }
];
// Convert to JSON
let jsonFruits = JSON.stringify(fruits);
console.log(jsonFruits);
// Convert back to JavaScript object
let jsFruits = JSON.parse(jsonFruits);
console.log(jsFruits);
Expected Output:
[{