Objects & Arrays
Two container types — objects and arrays — are all JSON needs to represent almost any real data shape, because you can nest them inside each other without limit. An object can hold arrays, arrays can hold objects, and those objects can hold more arrays. The skill worth building here isn't memorizing syntax — it's reading a nested structure fluently.
💡 The Bottom Line
Use an object when you're describing "one thing with named properties" (a person, a config). Use an array when you're describing "a list of things" (multiple people, a list of tags). Real-world JSON is almost always a mix of both, nested several levels deep.
A realistic nested example
{
"id": 101,
"name": "Ada Lovelace",
"active": true,
"roles": ["admin", "editor"],
"address": {
"city": "London",
"country": "UK"
},
"projects": [
{ "title": "Analytical Engine Notes", "year": 1843 },
{ "title": "First Algorithm", "year": 1843 }
]
}
Reading this from the outside in: the whole thing is one object (a person). It has simple
string/number/boolean properties, one property that's an array of strings (roles),
one that's a nested object (address), and one that's an array of objects
(projects) — the most common "list of records" shape you'll see in real APIs.
📋 Key differences between the two
- Object keys are named and unordered — you access a value by its key, not its position.
- Array items are positional and ordered — you access a value by its index (0, 1, 2, ...), and order is guaranteed to be preserved.
- An object's keys should be unique — if a key appears twice, most parsers keep only the last occurrence, silently.
⚠️ Watch Out
It's easy to reach for an object with numeric-looking keys ({"0": "a", "1": "b"})
when you really mean an array (["a", "b"]). If your data is a sequence where
order matters and you'd naturally count through it, it should almost always be an array, not
an object with number-like keys — the array form is what every JSON-aware tool expects for
that shape.
🙋 There Are No Dumb Questions
Q: Can a JSON array hold mixed types, like a string and a number together?
A: Yes — ["hello", 42, true] is completely valid JSON. Nothing enforces that every
element in an array be the same type, the way a typed array in some programming languages
might. It's up to the API or schema producing the JSON to decide whether mixing types in one
array is a good idea (usually, for readability, it isn't — but the format itself allows it).
🔧 Try It Yourself
Model a small blog post as JSON: a title (string), a published flag (boolean), an array of tag strings, and an array of comment objects, each with an author name and a message. Try going two levels deep — comments that themselves have an array of replies.
✅ Quick Check
You need to represent an ordered list of five blog post titles in JSON. What's the right shape?
Next up: turning real JSON text into usable objects in your code, and back again — parsing and stringifying.