JSON arrays are ordered collections of values enclosed in square brackets []. They can contain any valid JSON data type including strings, numbers, booleans, objects, or even other arrays.
Understanding JSON arrays is essential for working with lists of data, API responses, and configuration files. This guide covers everything you need to know about JSON arrays. You can alsovisualize JSON arrays using our interactive JSON viewer tool.
JSON Array Syntax
A JSON array starts with [ and ends with ], with values separated by commas:
[value1, value2, value3, ...]
Important: JSON arrays maintain insertion order. The first element is at index 0.
JSON Array Examples
Array of Strings
{
"protocols": ["SSH", "HTTPS", "SNMP", "FTP"]
}Array of Numbers
{
"ports": [22, 80, 443, 8080]
}Array of Objects
{
"devices": [
{
"hostname": "router-01",
"ipAddress": "192.168.1.1",
"active": true
},
{
"hostname": "switch-02",
"ipAddress": "192.168.1.2",
"active": true
}
]
}Mixed Data Types
{
"mixed": ["text", 123, true, null, {"key": "value"}]
}Accessing Array Elements
Use zero-based index to access elements. If you're working with JSON strings, first useJSON.parse() to convert them to JavaScript objects:
const data = {
"protocols": ["SSH", "HTTPS", "SNMP"]
};
console.log(data.protocols[0]); // "SSH"
console.log(data.protocols[1]); // "HTTPS"
console.log(data.protocols[2]); // "SNMP"
// Get array length
console.log(data.protocols.length); // 3Nested Arrays
Arrays can contain other arrays (multi-dimensional arrays):
{
"matrix": [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
}Accessing nested array elements:
const data = {
"matrix": [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
};
console.log(data.matrix[0][0]); // 1
console.log(data.matrix[1][2]); // 6
console.log(data.matrix[2][1]); // 8Iterating Over Arrays
Using for Loop
const devices = [
{ "hostname": "router-01" },
{ "hostname": "switch-02" }
];
for (let i = 0; i < devices.length; i++) {
console.log(devices[i].hostname);
}Using forEach
devices.forEach(device => {
console.log(device.hostname);
});Using map
const hostnames = devices.map(device => device.hostname); console.log(hostnames); // ["router-01", "switch-02"]
Modifying Arrays
Adding Elements
const protocols = ["SSH", "HTTPS"];
protocols.push("SNMP"); // Add to end
protocols.unshift("FTP"); // Add to beginning
console.log(protocols); // ["FTP", "SSH", "HTTPS", "SNMP"]Use JSON.stringify() to convert arrays back to JSON strings for storage or transmission.
Removing Elements
const protocols = ["FTP", "SSH", "HTTPS", "SNMP"]; protocols.pop(); // Remove from end protocols.shift(); // Remove from beginning console.log(protocols); // ["SSH", "HTTPS"]
Filtering Arrays
const devices = [
{ "hostname": "router-01", "active": true },
{ "hostname": "switch-02", "active": false },
{ "hostname": "firewall-03", "active": true }
];
const activeDevices = devices.filter(device => device.active);
console.log(activeDevices.length); // 2Common JSON Array Patterns
API Response with Array
{
"status": "success",
"data": [
{ "id": 1, "name": "Device A" },
{ "id": 2, "name": "Device B" }
],
"count": 2
}Pagination with Arrays
{
"items": [...],
"page": 1,
"totalPages": 10,
"totalItems": 100
}Configuration with Arrays
{
"servers": [
{
"host": "192.168.1.1",
"port": 443,
"protocols": ["https", "ssh"]
}
]
}Best Practices
- •Use consistent data types: Keep array elements of the same type when possible
- •Include metadata: Add count, page, or status fields alongside arrays
- •Avoid deep nesting: Keep arrays shallow for better readability and performance
- •Validate array data: Check for null/undefined before accessing elements
- •Use meaningful names: Name arrays descriptively (devices, users, items)
- •Consider performance: Large arrays can impact parsing and rendering speed
Common Mistakes to Avoid
Trailing Commas
JSON doesn't allow trailing commas:
// ❌ Invalid JSON ["item1", "item2", "item3",] // ✅ Valid JSON ["item1", "item2", "item3"]
Out of Bounds Access
Always check array length:
const arr = ["a", "b", "c"]; console.log(arr[10]); // undefined (no error, but unexpected)
JSON Tools
Continue Learning
External References
- •MDN - JavaScript Arrays - Complete array reference
- •JSON.org - JSON specification