JSON
JSON (JavaScript Object Notation) Breakdown
JSON is a lightweight data format used for data exchange between systems. It's text-based, human-readable, and follows a key-value structure similar to dictionaries in Python or objects in JavaScript.
1. JSON Data Structure
JSON consists of:
- Objects
{ key: value }(like dictionaries) - Arrays
[value1, value2](like lists) - Primitive Values (strings, numbers, booleans, null)
Example JSON
{
"name": "Alice",
"age": 25,
"isStudent": false,
"hobbies": ["reading", "gaming"],
"address": {
"city": "New York",
"zip": "10001"
}
}
2. JSON Data Types
JSON supports the following six data types:
| Type | Example | Notes |
|---|---|---|
| String | "hello" |
Enclosed in double quotes |
| Number | 25, 3.14 |
Integers & floating points |
| Boolean | true, false |
No quotes needed |
| Null | null |
Represents an empty value |
| Object | { "key": "value" } |
Collection of key-value pairs |
| Array | [1, 2, "text"] |
Ordered list of values |
3. JSON Objects ({})
A JSON object is a collection of key-value pairs.
Example
{
"id": 101,
"name": "Bob",
"email": "bob@example.com"
}
- Keys must be strings (double-quoted).
- Values can be any valid JSON type.
4. JSON Arrays ([])
JSON arrays are ordered collections of values.
Example
{
"fruits": ["apple", "banana", "cherry"]
}
- Arrays can contain mixed types, though it’s uncommon:
{
"mixed": [42, "hello", true, null]
}
5. Nesting Objects & Arrays
JSON supports nesting, meaning objects can contain arrays and vice versa.
Example
{
"user": {
"id": 1,
"name": "John",
"contacts": ["john@email.com", "123-456-7890"]
}
}
6. JSON Syntax Rules
- Use double quotes (
") for keys and strings. - No trailing commas (unlike JavaScript objects).
- Keys must be strings.
- Booleans and
nullare lowercase (true,false,null).
✅ Valid JSON:
{ "name": "Alice", "age": 30 }
❌ Invalid JSON (trailing comma):
{ "name": "Alice", "age": 30, }
7. JSON vs JavaScript Objects
| Feature | JSON Example | JavaScript Object Example |
|---|---|---|
| Key Quotes | { "name": "Alice" } |
{ name: "Alice" } (No quotes) |
| Property Access | jsonObj["name"] |
obj.name or obj["name"] |
| Allowed Values | Strings, numbers, arrays, objects | Functions, dates, regex |
8. Converting JSON in Different Languages
JavaScript
// Convert JSON string to object
let obj = JSON.parse('{"name": "Alice"}');
// Convert object to JSON string
let jsonStr = JSON.stringify(obj);
Python
import json
# Convert JSON string to dictionary
data = json.loads('{"name": "Alice"}')
# Convert dictionary to JSON string
json_str = json.dumps(data, indent=2)
9. Common JSON Operations
Reading JSON from a File
Python
with open("data.json", "r") as file:
data = json.load(file)
JavaScript (Node.js)
const fs = require('fs');
let data = JSON.parse(fs.readFileSync("data.json", "utf-8"));
10. Common Mistakes in JSON
| Mistake | Example | Fix |
|---|---|---|
| Single quotes | {'key': 'value'} |
Use double quotes (") |
| Trailing comma | { "name": "Alice", } |
Remove comma |
| Unquoted keys | { name: "Alice" } |
Use "name" |
| Functions | { "sayHello": function() { return "Hi"; } } |
Functions are not allowed |
11. JSON in APIs
JSON is widely used in REST APIs for data exchange.
Example API Response (GET Request)
{
"status": "success",
"data": {
"id": 1,
"name": "Alice"
}
}
Clients (browsers, mobile apps) parse this response to extract and display data.
12. JSON Schema (Validation)
To ensure JSON is structured correctly, JSON Schema is used.
Example JSON Schema
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name", "age"]
}
13. JSON Tools
- Online Formatters: jsonlint.com
-
CLI Pretty Print (Linux/Mac):
cat data.json | jq . -
PowerShell Pretty Print:
Get-Content data.json | ConvertFrom-Json | ConvertTo-Json -Depth 10
Key Takeaways
✅ JSON is simple and widely used.
✅ Always use double quotes for keys and strings.
✅ Objects ({}) store key-value pairs.
✅ Arrays ([]) store ordered values.
✅ JSON is not the same as JavaScript objects (no functions, strict syntax).
Do you need deeper insight into a specific part? 🚀