JSON Objects
Learning Focus
Objects are the most fundamental JSON container. Every real-world API payload is an object or an array of objects. Master the rules and patterns here.
What is a JSON Object?
An object is an unordered collection of zero or more key-value pairs wrapped in {}.
{
"id": 1001,
"username": "alice",
"email": "alice@example.com",
"createdAt": "2024-01-15T08:30:00Z",
"isActive": true,
"metadata": null
}
Rules
- Keys must be double-quoted strings
- Key-value pairs separated by
, - Key and value separated by
: - No trailing comma after the last pair
- Order is not guaranteed — parsers may reorder keys
- Avoid duplicate keys — parser behaviour is undefined
Nested Objects
{
"user": {
"id": 123,
"profile": {
"firstName": "Alice",
"address": {
"city": "Singapore",
"postal": "018989"
}
}
}
}
Language Mappings
| Language | JSON Object Maps To |
|---|---|
| JavaScript | {} object literal |
| Python | dict |
| Go | struct (typed) or map[string]interface{} |
| PHP | stdClass or associative array |
| Ruby | Hash |
Concept Map
Concept Flow
JSON Object → Unordered Key-Value Pairs
├── Keys: double-quoted strings
└── Values: any JSON type
└── Nesting allowed
└── Real-world API payloads
Common Pitfalls
| Pitfall | Consequence | Prevention |
|---|---|---|
| Duplicate keys | Parser-dependent behaviour | Ensure unique keys |
| Assuming key order | Breaks order-sensitive logic | Use arrays if order matters |
| Missing comma between pairs | Parse error | Use a linter or formatter |
| Trailing comma | Parse error | Remove or use JSONC parser |
What's Next
- Previous: Booleans and Null — Review literal types.
- Next: JSON Arrays — Learn the ordered list container.
- Section Overview — Return to the Data Types module index.