The Nesting Problem
APIs return data like this all the time. Accessing user.address.geo.lat is easy when the path exists — but what happens when any level is missing?
1{2 "user": {3 "id": 42,4 "profile": {5 "name": "Alice Chen",6 "address": {7 "street": "123 Main St",8 "city": "San Francisco",9 "geo": {10 "lat": 37.7749,11 "lng": -122.419412 }13 }14 },15 "preferences": {16 "theme": "dark",17 "notifications": {18 "email": true,19 "push": false20 }21 }22 }23}Safe Access Patterns
1. Optional Chaining (JavaScript)
1const city = data?.user?.profile?.address?.city;2// Returns "San Francisco" or undefined — never throws34const lat = data?.user?.profile?.address?.geo?.lat ?? 0;5// Provides a default value if any level is missing2. Dynamic Path Access
1function getByPath(obj, path) {2 return path.split('.').reduce(3 (current, key) => current?.[key],4 obj5 );6}78getByPath(data, 'user.profile.address.city');9// "San Francisco"1011getByPath(data, 'user.profile.address.zip');12// undefined (no error)3. Python — dict.get() Chaining
1# Safe access with .get() and default values2city = data.get("user", {}).get("profile", {}).get("address", {}).get("city", "Unknown")34# Or use a helper5def get_path(obj, path, default=None):6 for key in path.split("."):7 if isinstance(obj, dict):8 obj = obj.get(key)9 else:10 return default11 return obj if obj is not None else default1213get_path(data, "user.profile.address.city") # "San Francisco"Flattening Nested JSON
Flattening converts nested structures into a single-level object with dot-notation keys. This is essential for spreadsheet exports, search indexing, and analytics.
Nested
{ "user": { "name": "Alice", "address": { "city": "SF" } }}Flattened
{ "user.name": "Alice", "user.address.city": "SF"}1function flatten(obj, prefix = '', result = {}) {2 for (const [key, value] of Object.entries(obj)) {3 const path = prefix ? `${prefix}.${key}` : key;4 if (value && typeof value === 'object' && !Array.isArray(value)) {5 flatten(value, path, result);6 } else {7 result[path] = value;8 }9 }10 return result;11}1213flatten({ user: { name: "Alice", scores: [98, 87] } });14// { "user.name": "Alice", "user.scores": [98, 87] }Use Our Tool
Querying with JSONPath
JSONPath lets you extract values from nested JSON using expressions, similar to CSS selectors for HTML.
| Expression | Description | Result |
|---|---|---|
| $.user.profile.name | Direct path access | "Alice Chen" |
| $.user.preferences.* | All preferences values | "dark", {...} |
| $..city | Any key named "city" at any depth | "San Francisco" |
| $.users[0].name | First user name | "Alice" |
| $.users[*].email | All user emails | Array of emails |
| $.users[?(@.active)] | Users where active is true | Filtered array |
Try It Live
When to Flatten vs Keep Nested
| Use Case | Keep Nested | Flatten |
|---|---|---|
| API responses | ✓ | |
| Config files | ✓ | |
| Spreadsheet export | ✓ | |
| Search indexing | ✓ | |
| Analytics / BI | ✓ | |
| Database storage | ✓ (MongoDB/JSONB) | ✓ (SQL columns) |
| Form data | ✓ | |
| Logging | ✓ |
Try These Tools
Try It Yourself
This JSON has 4 levels of nesting. Try modifying the structure, adding deeper levels, or removing intermediate objects to see what happens.
Try It Yourself
Explore nested structure — modify and validate