JSON Object Example: Structure, Syntax & Usage
A complete guide to JSON objects — the fundamental building block of every JSON document. Learn the syntax, data types, and rules with real-world examples.
What Is a JSON Object?
A JSON object is an unordered collection of key-value pairs enclosed in curly braces {}. Every key must be a double-quoted string, and each key is followed by a colon and a value. Pairs are separated by commas. Objects are the most common top-level structure in JSON and appear everywhere — from API responses to configuration files.
Practical JSON Object Example
Below is a realistic user profile object that demonstrates several JSON data types working together:
{
"id": 42,
"username": "jane_doe",
"email": "jane@example.com",
"isActive": true,
"age": 29,
"balance": 1250.75,
"bio": null,
"roles": ["admin", "editor"],
"address": {
"street": "123 Main St",
"city": "Portland",
"state": "OR",
"zip": "97201"
},
"createdAt": "2024-08-15T10:30:00Z"
}Breaking Down the Structure
Let's walk through each data type present in the example above:
- String —
"username": "jane_doe". Always double-quoted. Single quotes are invalid in JSON. - Number —
"age": 29and"balance": 1250.75. Integers and decimals are both valid, but never quote them. - Boolean —
"isActive": true. Only lowercasetrueandfalseare valid. - Null —
"bio": null. Represents an intentionally empty value. Always lowercase. - Array —
"roles": ["admin", "editor"]. An ordered list of values enclosed in square brackets. - Nested Object —
"address": {...}. Objects can contain other objects, enabling hierarchical data.
JSON Object Rules
JSON has strict formatting rules. Unlike JavaScript object literals, JSON does not allow flexibility in syntax:
- Keys must be double-quoted strings. Unquoted keys or single-quoted keys will cause a parse error.
- No trailing commas. A comma after the last key-value pair is invalid.
- No comments. JSON has no comment syntax — use a separate field like
"_comment"if you must annotate. - Values must be valid JSON types. Functions,
undefined,NaN, andInfinityare not allowed. - Duplicate keys are technically allowed by the spec, but behavior is undefined — most parsers keep the last value. Avoid duplicate keys.
Nesting Objects
JSON objects can be nested as deep as you need. Here's an example with a nested settings object:
{
"user": "jane_doe",
"preferences": {
"theme": "dark",
"notifications": {
"email": true,
"sms": false,
"push": {
"enabled": true,
"sound": "default"
}
}
}
}While nesting is powerful, deeply nested structures become hard to read and maintain. A good rule of thumb is to keep nesting to three or four levels. If you go deeper, consider flattening the structure or breaking it into separate documents.
Common Mistakes to Avoid
These are the errors developers run into most often when writing JSON objects:
- Using single quotes:
{'name': 'Jane'}is invalid. Always use double quotes. - Trailing comma:
{"name": "Jane", "age": 29,}— that last comma will cause a parse error. - Unquoted keys:
{name: "Jane"}works in JavaScript but not in JSON. - Using undefined:
{"bio": undefined}is invalid. Usenullinstead. - Including comments: There is no
//or/* */in JSON.
When to Use JSON Objects
JSON objects are ideal when you need to represent a single entity with named properties — a user, a product, a configuration. If you're representing a collection of similar items, wrap them in an array. If you're representing a single record with distinct fields, an object is the right choice.
Objects also serve as the top-level container for most JSON documents. Even when your primary data is an array, wrapping it in an object (like {"data": [...]}) gives you room to add metadata like pagination or status codes later.
Try it yourself: Paste this example into our JSON Formatter to see it beautified and validated instantly.