Lesson 4.12: Introduction to JSON
JSON (JavaScript Object Notation) is a lightweight data format used for storing and exchanging data between a server and a web application. It is easy to read and write for humans and easy to parse for machines.
1. Why JSON?
-
Works perfectly with JavaScript.
-
Language-independent and widely used in APIs.
-
Simple format to store structured data like objects and arrays.
2. JSON Syntax
-
Data is represented as key-value pairs.
-
Keys must be strings in double quotes.
-
Values can be: string, number, boolean, array, object, or
null.
Example:
3. JSON vs JavaScript Object
-
JavaScript Object:
-
JSON:
Note: JSON keys must always be double-quoted strings.
4. Using JSON in JavaScript
a) Convert Object to JSON (stringify)
b) Convert JSON to Object (parse)
5. Practical Use
-
Fetch data from APIs:
-
Store structured data in LocalStorage:
✅ Summary:
-
JSON = Lightweight format for data storage & exchange.
-
Always use double quotes for keys.
-
Convert between JS objects ↔ JSON strings using
JSON.stringify()andJSON.parse(). -
Widely used in APIs, LocalStorage, and server communication.
