Skip to main content

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

  1. Keys must be double-quoted strings
  2. Key-value pairs separated by ,
  3. Key and value separated by :
  4. No trailing comma after the last pair
  5. Order is not guaranteed — parsers may reorder keys
  6. Avoid duplicate keys — parser behaviour is undefined

Nested Objects

{
"user": {
"id": 123,
"profile": {
"firstName": "Alice",
"address": {
"city": "Singapore",
"postal": "018989"
}
}
}
}

Language Mappings

LanguageJSON Object Maps To
JavaScript{} object literal
Pythondict
Gostruct (typed) or map[string]interface{}
PHPstdClass or associative array
RubyHash

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

PitfallConsequencePrevention
Duplicate keysParser-dependent behaviourEnsure unique keys
Assuming key orderBreaks order-sensitive logicUse arrays if order matters
Missing comma between pairsParse errorUse a linter or formatter
Trailing commaParse errorRemove or use JSONC parser

What's Next