JSON Syntax
Rules and structure for writing JSON
📋 JSON Syntax Rules
JSON syntax is derived from JavaScript object notation. It follows specific rules that make it easy to read and parse.
{
"name": "Alice",
"age": 25,
"active": true
}
Key Rules:
✓ Keys must be strings in double quotes
✓ Values can be strings, numbers, booleans, etc.
✓ Use commas to separate key-value pairs
Essential Syntax Rules
Double Quotes
Strings must use double quotes
"name": "John"
Curly Braces
Objects are wrapped in curly braces
{"key": "value"}
Square Brackets
Arrays use square brackets
["apple", "banana"]
Commas
Separate items with commas
"a": 1, "b": 2
🔹 Valid JSON Structure
Here's a properly formatted JSON object:
{
"student": {
"id": 12345,
"name": "Sarah Johnson",
"email": "[email protected]",
"enrolled": true,
"gpa": 3.85,
"courses": [
"Mathematics",
"Physics",
"Computer Science"
],
"address": {
"street": "456 Oak Avenue",
"city": "Boston",
"zipCode": "02101"
}
}
}
This JSON is valid because:
✓ All strings use double quotes
✓ Objects use curly braces {}
✓ Arrays use square brackets []
✓ Commas separate all items
✓ No trailing commas
🔹 Common Syntax Errors
Avoid these common mistakes:
❌ Wrong - Single Quotes
// INVALID
{
'name': 'John',
'age': 30
}
✅ Correct - Double Quotes
{
"name": "John",
"age": 30
}
❌ Wrong - Trailing Comma
// INVALID
{
"name": "John",
"age": 30,
}
✅ Correct - No Trailing Comma
{
"name": "John",
"age": 30
}
🔹 JSON Value Types
JSON supports these data types:
{
"string": "Hello World",
"number": 42,
"boolean": true,
"null_value": null,
"object": {
"nested": "value"
},
"array": [1, 2, 3, 4, 5]
}
Supported Types:
- String: Text in double quotes
- Number: Integer or decimal
- Boolean: true or false
- null: Represents empty value
- Object: Collection of key-value pairs
- Array: Ordered list of values
🔹 Nested JSON Example
JSON can contain nested objects and arrays:
{
"company": "Tech Corp",
"employees": [
{
"id": 1,
"name": "Alice Smith",
"department": "Engineering",
"skills": ["JavaScript", "Python", "React"],
"contact": {
"email": "[email protected]",
"phone": "+1-555-0123"
}
},
{
"id": 2,
"name": "Bob Wilson",
"department": "Design",
"skills": ["Photoshop", "Figma", "CSS"],
"contact": {
"email": "[email protected]",
"phone": "+1-555-0124"
}
}
]
}