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.
JSON Syntax
{
"name": "John Doe",
"age": 30,
"isActive": true,
"email": null,
"hobbies": ["reading", "coding", "gaming"],
"address": {
"street": "123 Main St",
"city": "New York"
}
}
Data Types
| Type | Example | Notes |
|---|
| String | "hello" | Must use double quotes |
|---|
| Number | 42, 3.14 | No leading zeros |
|---|
| Boolean | true, false | Lowercase only |
|---|
| Null | null | Lowercase only |
|---|
| Array | [1, 2, 3] | Ordered list |
|---|
| Object | {"key": "value"} | Unordered key-value pairs |
|---|
Common Mistakes
// ❌ Wrong: Single quotes
{'name': 'John'}
// ✅ Correct: Double quotes
{"name": "John"}
// ❌ Wrong: Trailing comma
{"name": "John",}
// ✅ Correct: No trailing comma
{"name": "John"}
// ❌ Wrong: Comments
{
"name": "John" // this is a comment
}
// ✅ JSON doesn't support comments
{"name": "John"}
JSON vs JavaScript Objects
// JavaScript object (more flexible)
const jsObj = {
name: 'John', // Unquoted keys OK
'full-name': 'John Doe', // Quoted keys OK
greet() { }, // Methods allowed
};
// JSON (stricter)
const json = {
"name": "John", // Keys must be quoted
"full-name": "John Doe" // Always double quotes
// No methods, no functions
};