Arrays Are the Backbone of Real-World JSON
Look at any modern API response and you will find arrays everywhere. Paginated results, list endpoints, batch operations, event streams, log records, telemetry batches, and product catalogs are all fundamentally arrays of objects. Correctly formatting a JSON array — indenting each item, preserving order, keeping the outer brackets on their own lines — is the single most common JSON-manipulation task in a developer's day.
The tool above handles four common shapes: (1) arrays of primitives like [1, 2, 3]; (2) arrays of objects with uniform shape (the paginated-API case); (3) arrays of objects with mixed shape (event streams); and (4) arrays of arrays (matrices, tabular data). Each shape has slightly different formatting best practices, all covered by the parser.
The One-Line-Per-Item Convention (JSONL)
For arrays of uniform objects — especially logs and events — the "one item per line" format has become the standard. This is JSON Lines (JSONL), also known as NDJSON (newline-delimited JSON). Each line is an independently-valid JSON object; the outer array wrapper is dropped. The wins: streaming ingestion (parse line-by-line without loading the full array into memory), diff-friendly output (a new event is one added line, not a restructured file), and error isolation (a single malformed line does not invalidate the rest).
Our formatter supports both shapes. Enable "compact items" to convert an array to JSONL-like output; disable it for standard indented JSON. Elasticsearch bulk API, Kafka message archives, and BigQuery loads all use JSONL — if you are feeding those systems, this option is the right choice.
Formatting Arrays in JavaScript
// Standard formatted output
const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
console.log(JSON.stringify(arr, null, 2));
// One-line-per-item (JSONL style)
console.log(arr.map(o => JSON.stringify(o)).join('\n'));
// Sort array of objects by a key, then format
const sorted = [...arr].sort((a, b) => a.name.localeCompare(b.name));
console.log(JSON.stringify(sorted, null, 2));
// Filter, then format
const active = arr.filter(o => o.active);
console.log(JSON.stringify(active, null, 2));Formatting Arrays in Python
import json
# Standard indent
arr = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
print(json.dumps(arr, indent=2))
# JSONL / NDJSON — one line per item
for item in arr:
print(json.dumps(item))
# Sort keys inside each object AND array by 'name' key
sorted_arr = sorted(arr, key=lambda x: x['name'])
print(json.dumps(sorted_arr, indent=2, sort_keys=True))
# Streaming write to file (memory-efficient for huge arrays)
with open('out.jsonl', 'w') as f:
for item in arr:
f.write(json.dumps(item) + '\n')Arrays With Mixed Types
JSON arrays can hold any mix of value types: [1, "text", true, null, , []] is a fully valid array. Most modern schema validators (JSON Schema, TypeScript) will complain about this, but at the JSON parser level anything goes. Our formatter handles mixed arrays cleanly — primitives stay inline, complex values break to multiple lines with correct indentation for their type.
The common real-world case is heterogeneous event streams: an array where each object has a type field and different fields based on type. The formatter treats each object individually so field ordering follows source order and no false consolidation happens.
Nested Arrays and Matrices
Arrays of arrays — matrices — are common in geospatial data (GeoJSON coordinate arrays), machine learning (weight matrices exported to JSON), and tabular exports. Our formatter recognizes deep array structure and applies indentation at every depth. Nested arrays where all elements are primitives are compacted to a single line when short (under 80 characters), which is the same convention Prettier and jq use.
Common Pitfalls
1. Trailing commas. Standard JSON does not allow trailing commas: [1, 2, 3,] is invalid, unlike JavaScript. If your input comes from a JavaScript source file or a lenient parser, strip trailing commas before formatting. JSON5 allows them; standard JSON does not.
2. Single-item arrays looking like non-arrays. XML APIs frequently strip the array wrapper when there is only one item: {"items": [x]} becomes {"items": x}. If you are consuming from a source with this behavior, always coerce to array before formatting: const list = Array.isArray(items) ? items : [items];.
3. Comments in "JSON" input. Standard JSON has no comments. If you paste a file with // comments or /* ... */ blocks (common in tsconfig.json), the parser will reject it. Strip comments first or use a JSON5-aware parser.
Related JSON Tools
- JSON Formatter (Parent Tool)
- Format JSON Online — generic beautifier
- JSON Line Formatter — JSONL-focused variant
- Sort JSON Keys — for canonical output
- JSON to CSV Converter — export arrays to spreadsheets