Learn/Data Structures

JSON Objects

Objects are the backbone of JSON. They store data as key-value pairs and can nest to represent complex hierarchical structures. Almost every API response, config file, and data record is a JSON object.

Beginner~12 min read

What is a JSON Object?

A JSON object is an unordered collection of key-value pairs enclosed in curly braces{}. Each key is a string, and each value can be any JSON type.

Basic object structurejson
1{
2 "key": "value",
3 "anotherKey": 42,
4 "nested": {
5 "inner": true
6 }
7}

Key-Value Pairs

Every pair follows the pattern "key": value with a colon separator:

Anatomy of a Key-Value Pair

Key Naming Conventions

camelCase

Example: "firstName"

Used by: JavaScript, TypeScript, Java

snake_case

Example: "first_name"

Used by: Python, Ruby, PostgreSQL

kebab-case

Example: "first-name"

Used by: CSS-like configs, HTTP headers

PascalCase

Example: "FirstName"

Used by: C#, .NET APIs

Nesting Objects

Objects can contain other objects, creating hierarchical data structures. This is how JSON represents complex real-world entities.

Nested object: API user responsejson
1{
2 "user": {
3 "id": 42,
4 "profile": {
5 "displayName": "Alice Chen",
6 "avatar": "https://cdn.example.com/alice.jpg",
7 "bio": "Full-stack developer"
8 },
9 "settings": {
10 "theme": "dark",
11 "language": "en",
12 "notifications": {
13 "email": true,
14 "push": false,
15 "sms": false
16 }
17 }
18 }
19}

Nesting Depth

Keep nesting to 3-4 levels maximum. Beyond that, data becomes hard to read, query, and maintain. Consider flattening deeply nested structures or using references.

Real-World Patterns

1. REST API Response

Paginated API responsejson
1{
2 "data": [
3 { "id": 1, "title": "First Post" },
4 { "id": 2, "title": "Second Post" }
5 ],
6 "pagination": {
7 "page": 1,
8 "perPage": 20,
9 "total": 145,
10 "totalPages": 8
11 },
12 "meta": {
13 "requestId": "req_abc123",
14 "timestamp": "2026-03-28T12:00:00Z"
15 }
16}

2. package.json Configuration

Node.js project configurationjson
1{
2 "name": "my-app",
3 "version": "1.0.0",
4 "scripts": {
5 "dev": "next dev",
6 "build": "next build",
7 "start": "next start"
8 },
9 "dependencies": {
10 "next": "^15.0.0",
11 "react": "^19.0.0"
12 }
13}

3. Error Response

Standardized API errorjson
1{
2 "error": {
3 "code": "VALIDATION_ERROR",
4 "message": "Request body is invalid",
5 "details": [
6 {
7 "field": "email",
8 "issue": "Invalid email format",
9 "received": "not-an-email"
10 }
11 ]
12 }
13}

Try It Yourself

Build a JSON object that represents a product in an e-commerce catalog. Include name, price, description, categories (array), and availability (boolean).

Try It Yourself

Modify or extend this product object

Frequently Asked Questions

Are JSON object keys ordered?
The JSON specification says objects are "unordered." However, most modern parsers (JavaScript, Python 3.7+) preserve insertion order. You should not rely on key order for correctness.
Can JSON objects have duplicate keys?
The spec does not explicitly forbid duplicate keys, but behavior is undefined. Most parsers keep the last value. Always use unique keys.
How deeply can JSON objects be nested?
There is no spec limit. Practically, 3-5 levels is recommended. Deeply nested JSON is hard to read and process. Consider flattening beyond 5 levels.
Can JSON object keys contain spaces?
Yes. Any valid JSON string can be a key: {"first name": "John"} is valid. However, keys with spaces are harder to access in code, so camelCase or snake_case is preferred.
What is the difference between JSON object and JavaScript object?
JSON objects require double-quoted keys, cannot contain functions or undefined, and do not support trailing commas or comments. JavaScript objects have none of these restrictions.