A JSON array of objects is the most common data structure you'll work with in APIs. Understanding how to create, access, filter, and transform them is fundamental to modern development.
What is a JSON array of objects?
[
{ "id": 1, "name": "Alice", "age": 28, "role": "developer" },
{ "id": 2, "name": "Bob", "age": 34, "role": "designer" },
{ "id": 3, "name": "Carol", "age": 25, "role": "developer" }
]
Each item in the array is a JSON object with the same structure. The outer [] makes it an array; the inner {} makes each item an object.
Access items in JavaScript
const users = [
{ id: 1, name: 'Alice', age: 28 },
{ id: 2, name: 'Bob', age: 34 }
];
users[0] // { id: 1, name: 'Alice', age: 28 }
users[0].name // 'Alice'
users.length // 2
users.at(-1).name // 'Bob' (last item)
Loop through the array
// for...of (recommended)
for (const user of users) {
console.log(user.name, user.age);
}
// forEach
users.forEach(user => console.log(user.name));
// map — transform into new array
const names = users.map(u => u.name);
// ['Alice', 'Bob']
Filter, find, sort
// filter — get subset
const developers = users.filter(u => u.role === 'developer');
// find — first match
const alice = users.find(u => u.name === 'Alice');
// findIndex — index of first match
const idx = users.findIndex(u => u.id === 2);
// sort — by age ascending
const sorted = [...users].sort((a, b) => a.age - b.age);
// sort — by name alphabetically
const byName = [...users].sort((a, b) => a.name.localeCompare(b.name));
Transform the array
// Add a field to every item
const withFullName = users.map(u => ({
...u,
displayName: `${u.name} (${u.role})`
}));
// Extract one field as a plain array
const ids = users.map(u => u.id); // [1, 2, 3]
// Convert array to object keyed by id
const byId = Object.fromEntries(users.map(u => [u.id, u]));
// { 1: {id:1,...}, 2: {id:2,...} }
// Group by role
const byRole = users.reduce((acc, u) => {
(acc[u.role] ??= []).push(u);
return acc;
}, {});
Work with JSON arrays in Python
import json
json_str = '[{"id":1,"name":"Alice","age":28},{"id":2,"name":"Bob","age":34}]'
users = json.loads(json_str)
# Access
print(users[0]['name']) # Alice
print(users[-1]['name']) # Bob (last item)
# Filter
developers = [u for u in users if u.get('role') == 'developer']
# Sort by age
sorted_users = sorted(users, key=lambda u: u['age'])
# Extract names
names = [u['name'] for u in users] # ['Alice', 'Bob']Try it free — JSON Formatter
Format and explore your JSON arrays instantly.