Nearby lessons
16 of 21📄 Node.js – Importing JSON
📌 What is JSON?
What is JSON?
JSON stands for JavaScript Object Notation. It is a standard, text-based, human-readable data interchange format used to exchange data between web clients and web servers.
- Lightweight format for storing and transporting data.
- Often used when data is sent from a server to a web page.
- Self-describing and easy to understand.
- Collection of key-value pairs where keys are strings and values can be: Number, String, Boolean, Array, Object, or NULL.
✨ Advantages of JSON
- Supported by all programming languages
- Language independent
- Can be used on all platforms
- Easy to read and understand
⚙️ JSON Methods in JavaScript
JSON.parse()→ Converts a JSON string into a JavaScript object.JSON.stringify()→ Converts a JavaScript object into a JSON string.
📌 Example JSON Structure
{
"id": 101,
"name": "John Doe",
"isActive": true,
"hobbies": ["reading", "traveling", "swimming"],
"address": {
"street": "123 Main St",
"city": "Anytown",
"postalCode": "12345"
},
"phoneNumber": null
}
💻 Importing JSON in Node.js
Node.js Environment
RUNTIME ACTIVE
const data = require('./data.json'); // Accessing JSON data console.log(data.id); console.log(data.name); console.log(data.hobbies); console.log(data.hobbies[1]); console.log(data.address); console.log(data.address.city); console.log(data.address.postalCode);
Terminal Output
🧠 Test Your Knowledge
4 QuestionsProgress: 0 / 4
Keep Going!Callback Functions in Node.js