JSON objects — syntax, nesting, and common mistakes
How JSON objects work: key rules, nesting, accessing values in JavaScript and Python, duplicate keys, and the mistakes that cause parse errors.
What is a JSON object?
A JSON object is an unordered collection of key/value pairs wrapped in curly braces — for example {"name": "Ada", "age": 36}. Keys must be double-quoted strings, each key is separated from its value by a colon, pairs are separated by commas, and there is no trailing comma after the last pair.
Syntax rules
{ "key": value, "key2": value2 }- Curly braces wrap the object: { and }
- Keys must be strings in double quotes — not single quotes, not bare words
- A colon separates each key from its value
- Commas separate pairs; no trailing comma after the last one
- Values may be any JSON type: string, number, boolean, null, array, or object
- An empty object {} is valid
Examples
// Simple
{ "name": "Ada", "age": 36, "active": true }
// Nested objects
{
"user": {
"name": "Ada",
"address": { "city": "London", "zip": "SW1A" }
}
}
// Object containing an array of objects
{
"team": "platform",
"members": [
{ "id": 1, "name": "Ada" },
{ "id": 2, "name": "Grace" }
]
}
// Empty object
{}Accessing values
// JavaScript
const data = JSON.parse('{"user":{"name":"Ada","tags":["a","b"]}}');
data.user.name; // "Ada"
data.user.tags[1]; // "b"
data.user?.address?.city; // undefined — safe optional chaining
Object.keys(data.user); // ["name", "tags"]
# Python
import json
data = json.loads('{"user":{"name":"Ada","tags":["a","b"]}}')
data["user"]["name"] # "Ada"
data["user"].get("address") # None — no KeyError
list(data["user"].keys()) # ['name', 'tags']Common mistakes
Unquoted or single-quoted keys
// ✗ Invalid — bare key (this is a JS object literal, not JSON)
{ name: "Ada" }
// ✗ Invalid — single quotes
{ 'name': 'Ada' }
// ✓ Valid
{ "name": "Ada" }Trailing comma
// ✗ Invalid
{ "a": 1, "b": 2, }
// ✓ Valid
{ "a": 1, "b": 2 }Duplicate keys
Legal but dangerous — most parsers silently keep the last value. See duplicate keys in JSON for what each parser does.
{ "id": 1, "id": 2 } // parses to { "id": 2 } in most languagesPython/JS literals that aren't JSON
// ✗ None / True / False are Python, not JSON
{ "value": None, "ok": True }
// ✓ JSON uses lowercase and null
{ "value": null, "ok": true }Objects vs arrays — which to use
Use an object when each value has a meaningful name. Use an array when you have a list of like items and order matters. A very common API shape combines both: an object of metadata wrapping an array of records.
{
"total": 2,
"page": 1,
"results": [
{ "id": 1, "name": "Ada" },
{ "id": 2, "name": "Grace" }
]
}See the companion guide on JSON arrays for the list side of this.
Explore the structure in the tree viewer →
Expand nested arrays and objects, see every type at a glance, and click any node to copy its JSONPath.
Open Tree Viewer →