Minify JSON API Responses

Minifying JSON removes all unnecessary whitespace to reduce payload size. For APIs that return large datasets, minification + gzip can reduce response size by 70–80%, significantly improving load times and reducing bandwidth costs.

Pretty-Printed vs Minified JSON

Pretty-printed (development) — 120 bytes

{
  "id": 1,
  "name": "Alice",
  "role": "admin",
  "active": true
}

Minified (production) — 51 bytes

{"id":1,"name":"Alice","role":"admin","active":true}

57% smaller — and that's before gzip compression, which reduces it by another 50–70%.

How to Minify JSON in Your API — By Language

JavaScript / Node.js

// JSON.stringify() with no indent = minified by default
const data = { id: 1, name: "Alice", role: "admin" };

// Minified (no whitespace)
JSON.stringify(data);
// '{"id":1,"name":"Alice","role":"admin"}'

// Pretty-printed (for development)
JSON.stringify(data, null, 2);

// Express.js — disable pretty-printing in production
app.set('json spaces', 0); // default, already compact

// In Next.js API routes:
export default function handler(req, res) {
    res.json(data); // already sends compact JSON
}

Python

import json

data = {"id": 1, "name": "Alice", "role": "admin"}

# Minified (no spaces after separators)
json.dumps(data)
# '{"id": 1, "name": "Alice", "role": "admin"}'

# Truly compact (remove spaces after , and :)
json.dumps(data, separators=(',', ':'))
# '{"id":1,"name":"Alice","role":"admin"}'

# Flask — compact by default in production
from flask import Flask, jsonify
app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False

# FastAPI — compact by default
# JSONResponse uses json.dumps which is compact

Go

import "encoding/json"

type User struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

user := User{ID: 1, Name: "Alice"}

// Minified (json.Marshal is compact by default)
data, _ := json.Marshal(user)
// {"id":1,"name":"Alice"}

// Pretty-printed (development only)
data, _ = json.MarshalIndent(user, "", "  ")

// HTTP handler — compact by default
func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(user) // compact output
}

Command Line with jq

# Minify a JSON file (-c = compact)
jq -c . pretty.json > minified.json

# Minify piped JSON
cat response.json | jq -c .

# Minify using Python
python3 -c "import json,sys; print(json.dumps(json.load(open(sys.argv[1])),separators=(',',':')))" input.json

# Node.js one-liner
node -e "const d=require('./input.json');process.stdout.write(JSON.stringify(d));" > output.json

Performance Tips Beyond Minification

compress

Enable gzip / Brotli compression

Far more impactful than minification alone. Most web servers (nginx, Apache, Caddy) enable gzip with one config line. Reduces JSON by 60–85% on typical API responses.

delete_sweep

Remove null and empty fields

Use omitempty in Go, separators in Python, or a JSON replacer function in JavaScript to exclude null values and empty arrays from the response.

tune

Return only requested fields

Implement field selection (like GraphQL or ?fields= query params) so clients only receive the properties they need. A users list with 5 fields instead of 30 is dramatically smaller.

cached

Cache minified responses

Minify once, cache the result. For static or infrequently-changing data, compute the minified response at build time or cache it with Redis/Memcached and serve directly.

FAQ

How much does minifying JSON reduce file size?expand_more

Minifying JSON typically reduces size by 15–30% by removing whitespace and newlines. Combined with gzip/Brotli compression (which all modern web servers support), the total reduction is often 60–80% compared to uncompressed pretty-printed JSON.

Should I minify JSON in my API responses?expand_more

Yes, for production APIs. Minified JSON reduces bandwidth usage and speeds up parsing on the client side. However, always enable gzip or Brotli compression on your server — it has a much larger impact than minification alone and handles both whitespace and repetition.

Does JSON.stringify() in JavaScript produce minified JSON?expand_more

Yes. JSON.stringify(obj) with no third argument produces compact, minified JSON. Only JSON.stringify(obj, null, 2) (or other indent values) adds whitespace and newlines. So by default, JSON.stringify() already outputs minified JSON.

How do I minify a JSON file on the command line?expand_more

Use jq -c . input.json > output.json (the -c flag means compact). Or with Python: python3 -c "import json,sys; print(json.dumps(json.load(open(sys.argv[1]))))" input.json > output.json

Minify JSON API Response – Reduce JSON Payload Size Online