A large JSON response slows down every client that fetches it, especially on mobile networks. The first idea is usually to minify it. That helps, but on a server that already compresses responses it helps far less than the other options. This article shows how to measure a payload and which changes make the biggest difference.
Measure before you optimize
There are three sizes to look at, and they tell different stories:
- Raw size: the JSON text as your code produces it.
- Transfer size: what travels over the network after
gziporbr(Brotli) compression. - Parsed cost: the time and memory the client needs to parse and keep the data.
In Chrome DevTools, the Network panel shows the transfer size and, in the tooltip or the "Size" column, the resource size. From the command line:
# Uncompressed size
curl -s https://api.example.com/items | wc -c
# Size as sent with compression
curl -s -H 'Accept-Encoding: br, gzip' -o /dev/null -w '%{size_download}\n' https://api.example.com/items
In Node.js you can compare all variants of a local file with the built-in zlib module:
import { readFileSync } from "node:fs";
import { gzipSync, brotliCompressSync } from "node:zlib";
const pretty = readFileSync("items.json", "utf8");
const compact = JSON.stringify(JSON.parse(pretty));
for (const [name, text] of [["pretty", pretty], ["minified", compact]]) {
console.log(name, text.length, "gzip", gzipSync(text).length, "brotli", brotliCompressSync(text).length);
}
Run it on your own data. The exact numbers depend on how deeply the JSON is nested and how repetitive it is, so any percentage from someone else's benchmark is only a rough guide.
What minifying does, and why compression shrinks the gain
Minifying removes spaces, tabs and line breaks between tokens. On pretty-printed, deeply nested JSON that can be a large share of the raw size.
Compression algorithms, however, are very good at exactly that kind of redundancy: long runs of the same indentation compress to almost nothing. So after gzip, the gap between pretty and minified JSON is small. Minify anyway when you control the output, because it costs nothing, but treat it as a finishing touch rather than the fix.
Turn on compression first
If responses go out uncompressed, this is the single biggest win. Most hosting platforms and CDNs compress application/json automatically. In your own servers:
// Express
import compression from "compression";
app.use(compression());
# nginx
gzip on;
gzip_types application/json;
Check the result: the response should carry a content-encoding: gzip or content-encoding: br header. Very small responses, under about a kilobyte, gain little from compression, and many servers skip them on purpose.
Send less data
Compression can't remove information. These changes do, and on real APIs they often matter most:
Paginate large lists
Returning 10,000 records when the screen shows 50 wastes bandwidth, server time and client memory. Use limit and a cursor (?limit=50&after=abc123), and include the next cursor in the response.
Return only the fields the client uses
A list endpoint rarely needs every column of every record. Either add a field selection parameter (?fields=id,name,updatedAt), offer a smaller "summary" representation for lists, or use GraphQL, where the client asks for fields explicitly.
Drop nulls and defaults
"middleName": null repeated across thousands of records adds up. Omitting keys whose value is null or a known default is safe as long as clients treat a missing key the same way. In JavaScript, a replacer does it while serializing:
JSON.stringify(data, (key, value) => (value === null ? undefined : value));
Document the change, and check that consuming code uses ?. or defaults rather than expecting the key to exist.
Avoid repeating nested objects
If 500 orders each embed the full customer object, the same data is sent 500 times. Send customer IDs in the list and the customers once, keyed by ID, or let the client fetch them separately and cache them.
Formats and encodings
- Numbers as strings: sending large IDs as strings is right for precision, but sending every number as a string makes payloads larger and clients slower. Keep numbers as numbers unless they exceed 2^53.
- Base64 inside JSON: embedding files as Base64 makes them about a third larger before compression. Upload and download binary data separately and put a URL in the JSON.
- Binary formats: MessagePack, CBOR or Protocol Buffers can be smaller and faster to parse, at the cost of readability and tooling. They're worth considering for high-volume internal services, rarely for public APIs.
Cache what doesn't change
The smallest response is the one that isn't sent. Set ETag or Last-Modified headers so clients can revalidate and receive a 304 Not Modified with no body, and Cache-Control for data that's safe to reuse for a while.
A checklist
- Confirm responses are compressed (
content-encodingheader). - Paginate every list endpoint.
- Return only the fields the client needs.
- Omit nulls and repeated nested objects.
- Send compact JSON from production APIs; keep pretty printing for logs and debugging.
- Use caching headers so unchanged data isn't sent again.
When you need to read one of those compact responses, a pretty printer turns it back into something readable.