Lesson 4 — Structure
Objects and Nesting
JSON objects can contain other objects and arrays as values, letting you model rich, hierarchical data structures of arbitrary depth.
Nesting Objects
A value in a JSON key-value pair can itself be an object, letting you group related fields together logically — an address inside a person, for example.
JSON — Nested objects
{
"name": "Kabelo",
"address": {
"street": "12 Main Road",
"city": "Johannesburg",
"postalCode": "2000"
},
"employer": {
"name": "Acme Corp",
"location": {
"city": "Cape Town",
"country": "South Africa"
}
}
}
Common Mistakes
- Losing track of matching braces in deeply nested structures, making it hard to see where one object ends and another begins.
- Flattening data that should logically be nested (e.g. streetAddress, cityName as top-level fields) instead of grouping it into a sub-object.
- Over-nesting simple data unnecessarily, making the structure harder to navigate than it needs to be.
- Forgetting a comma between sibling key-value pairs at the same nesting level.
Professional Tip
Use a code editor with bracket-matching or JSON-aware syntax highlighting while working with deeply nested structures — it makes it immediately obvious which closing brace belongs to which opening one.
Your Turn
Model a JSON object for a company that has a name and a nested array of employees, each employee itself having a name and a nested object for their contact details (email and phone).
Mini Quiz
Can a JSON object's value be another JSON object?
JSON supports arbitrary nesting — objects can contain other objects (and arrays) as values, which is how complex, hierarchical data is represented.