JSON Object Tutorial
A JSON object is an unordered collection of key-value pairs enclosed in curly braces. It is the primary structure for representing real-world entities in JSON — from API responses to configuration files.
Basic syntax
Each key must be a double-quoted string, followed by a colon and a value:
{
"name": "Alice",
"age": 30,
"active": true
}Rules:
- Keys must be double-quoted strings
- Key-value pairs are separated by commas
- No trailing comma after the last pair
- Values can be strings, numbers, booleans, null, arrays, or other objects
All value types in a JSON object
{
"string": "hello",
"integer": 42,
"float": 3.14,
"boolean": true,
"null": null,
"array": [1, 2, 3],
"object": { "nested": "value" }
}Nested JSON objects
Objects can be nested at any depth. Each nested object follows the same rules as the top-level object.
{
"company": {
"name": "Acme Corp",
"address": {
"street": "123 Main St",
"city": "Springfield",
"country": "US"
},
"employees": 150
}
}Empty object
An empty object is valid JSON:
{}Often used as a default or placeholder value in APIs.
Access object properties in JavaScript
const json = '{"name": "Alice", "age": 30, "address": {"city": "NYC"}}';
const data = JSON.parse(json);
// Dot notation
console.log(data.name); // "Alice"
console.log(data.address.city); // "NYC"
// Bracket notation (required for dynamic keys or special chars)
console.log(data["age"]); // 30
// Optional chaining (safe for nested access)
console.log(data?.address?.zip); // undefined (no error if missing)
// Destructuring
const { name, age } = data;
console.log(name, age); // Alice 30
// Check if a key exists
console.log("name" in data); // true
console.log(data.hasOwnProperty("email")); // falseIterate over object keys in JavaScript
const data = { name: "Alice", age: 30, active: true };
// Object.keys() — array of keys
Object.keys(data).forEach(key => {
console.log(key, data[key]);
});
// Object.entries() — array of [key, value] pairs
for (const [key, value] of Object.entries(data)) {
console.log(`${key}: ${value}`);
}
// Object.values() — array of values
const values = Object.values(data);
console.log(values); // ["Alice", 30, true]Access object properties in Python
import json
json_string = '{"name": "Alice", "age": 30, "address": {"city": "NYC"}}'
data = json.loads(json_string)
# Direct access
print(data["name"]) # Alice
# Safe access with .get()
print(data.get("email", "not set")) # not set
# Nested access
print(data["address"]["city"]) # NYC
# Check key existence
print("name" in data) # TrueIterate over a JSON object in Python
import json
data = json.loads('{"name": "Alice", "age": 30, "active": true}')
# Iterate keys
for key in data:
print(key, data[key])
# Iterate key-value pairs
for key, value in data.items():
print(f"{key}: {value}")
# Get all keys and values
keys = list(data.keys())
values = list(data.values())Merge JSON objects in JavaScript
const defaults = { theme: "dark", language: "en", timeout: 30 };
const userPrefs = { theme: "light", fontSize: 14 };
// Spread operator (userPrefs overrides defaults)
const config = { ...defaults, ...userPrefs };
console.log(config);
// { theme: "light", language: "en", timeout: 30, fontSize: 14 }
// Object.assign() — same result
const config2 = Object.assign({}, defaults, userPrefs);Common mistakes
- Single-quoted keys —
{"key": "val"}is valid;{'key': 'val'}is not valid JSON. - Unquoted keys —
{key: "val"}is JavaScript object literal syntax, not JSON. - Trailing comma —
{"a": 1, "b": 2,}is invalid. - Duplicate keys — JSON allows duplicate keys but behavior is undefined. Most parsers keep the last value.
Format and validate JSON objects online
Use the JSON Formatter to format and validate any JSON object, or the tree view to explore nested structures visually. For enforcing a specific object structure, use the JSON Schema Validator.