如何压缩JSON?面试JSON响应压缩问题及实现原理求解
Great question! Compressing JSON responses is a super common optimization to cut down payload sizes and speed up API calls—let’s break down all the main methods, how they work, and use your sample JSON to make it concrete. First, a quick note: your original example needs double quotes around keys to be valid JSON, so we’ll use this corrected version as our starting point:
{ "name": "sample name", "product": "sample product", "address": "sample address" }
1. JSON Minification (Basic Text Compression)
What it is: The simplest form of compression that strips out all unnecessary whitespace (spaces, newlines, tabs) while keeping valid JSON syntax. Any standard JSON parser can read the minified output without extra work.
Minified version of your sample:
{"name":"sample name","product":"sample product","address":"sample address"}
How it works:
JSON ignores whitespace outside of string values, so removing these characters reduces payload size without losing any data. Most languages have built-in or library support for this:
- JavaScript: Use
JSON.stringify(yourObject, null, 0)(the third parameter0disables formatting) - Python: Use
json.dumps(yourObject, separators=(',', ':')) - Java: Use Jackson with
SerializationFeature.INDENT_OUTPUTdisabled
2. HTTP-Level Compression (Gzip/Brotli)
What it is: Apply general-purpose lossless compression algorithms to the entire JSON payload (minified or not) at the HTTP layer. This gives way better compression ratios than minification alone, especially for larger payloads.
How it works:
Algorithms like Gzip and Brotli work by:
- Scanning the data for repeated sequences (e.g., the word "sample" appears three times in your example)
- Replacing repeated sequences with shorter references
- Storing a dictionary of these references to reconstruct the original data during decompression
Implementation Steps:
- On the server: Enable compression in your web server or backend framework. For example:
- Nginx: Add
gzip on;orbrotli on;to your config (Brotli generally delivers better compression than Gzip) - Node.js: Use the
compressionmiddleware - Python/Flask: Use the
flask-compressextension
- Nginx: Add
- On the client: Modern browsers automatically send an
Accept-Encoding: gzip, deflate, brheader, telling the server it can accept compressed responses. The server responds with aContent-Encoding: gzip(orbr) header, and the browser automatically decompresses the payload before parsing it as JSON.
3. Structural Compression (Custom Optimizations)
For bandwidth-sensitive use cases (like mobile apps), you can optimize the JSON structure itself for even smaller payloads:
a. Shorten Key Names
Replace long, descriptive keys with shorter aliases, then map them back during parsing. This works best for JSON with consistent, predictable structures.
Compressed version of your sample:
{"n":"sample name","p":"sample product","a":"sample address"}
You’ll just need to agree on the alias mapping with your frontend/consumer to parse this correctly.
b. Binary JSON Formats
Convert JSON to a binary format like MessagePack or BSON, which are designed to be more compact than text-based JSON.
How it works:
These formats encode JSON data types (strings, numbers, objects) into binary values that take up less space. For example:
- A string’s length is stored as a single byte (instead of text digits)
- Numbers are stored in their native binary form (no text conversion overhead)
Most languages have libraries to serialize/deserialize these formats (e.g., msgpack for Python, msgpack-lite for JavaScript).
Summary of Recommendations
- Start with minification—it’s easy to implement and gives quick wins.
- Add Gzip/Brotli compression—this is the most impactful step for most APIs, with minimal effort.
- For high-volume or bandwidth-sensitive use cases, consider structural compression or binary formats.
内容的提问来源于stack exchange,提问作者Vignesh

