JSON Introduction
Understanding JavaScript Object Notation
📄 What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It's easy for humans to read and write, and easy for machines to parse and generate.
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
This represents:
Name: John Doe
Age: 30
City: New York
Key JSON Concepts
Key-Value Pairs
Data is stored in key-value pairs
"name": "Alice"
Text Format
JSON is written in plain text
{"message": "Hello World"}
Language Independent
Works with most programming languages
{"supported": true}
Lightweight
Minimal syntax, fast to process
{"fast": true, "size": "small"}
🔹 JSON Structure
JSON is built on two main structures:
{
"object": {
"key1": "value1",
"key2": "value2"
},
"array": [
"item1",
"item2",
"item3"
]
}
Explanation:
Objects: Collection of key-value pairs in curly braces {}
Arrays: Ordered list of values in square brackets []
🔹 Simple JSON Example
Here's a basic JSON object representing a person:
{
"firstName": "Emma",
"lastName": "Watson",
"age": 33,
"isStudent": false,
"address": {
"street": "123 Main St",
"city": "London",
"country": "UK"
},
"hobbies": ["reading", "acting", "activism"]
}
This JSON contains:
Strings: "Emma", "Watson", "London"
Number: 33
Boolean: false
Object: address information
Array: list of hobbies
🔹 Why Use JSON?
JSON is popular because it's:
- Easy to read: Human-friendly format
- Lightweight: Less data overhead than XML
- Fast: Quick to parse and generate
- Universal: Supported by all modern languages
- Web-friendly: Native JavaScript support
{
"api_response": {
"status": "success",
"data": {
"user_count": 1250,
"last_updated": "2024-01-15"
}
}
}