This is a simple data compressor for JSON data in API responses. It will analyze the JSON structure and transform the JS objects into arrays to reduce the size of the response.
Note: This compressor is designed for JSON structured optimizations, not a real compression algorithm like gzip.
npm install api-data-compressor
import { compress } from 'api-data-compressor';
const data = [{
id: 1,
name: 'John Doe',
address: {
street: '123 Main St',
city: 'Springfield',
state: 'IL',
zip: '62701'
},
phone: '555-555-5555',
email: 'john.doe@google.com'
}, {
id: 2,
name: 'Jane Smith',
address: {
street: '456 Elm St',
city: 'Springfield',
state: 'IL',
zip: '62701'
},
phone: '555-555-5555',
email: 'jane.smith@google.com'
}, {
id: 3,
name: 'Bob Johnson',
address: {
street: '789 Oak St',
city: 'Springfield',
state: 'IL',
zip: '62701'
},
phone: '555-555-5555',
email: 'bob.johnson@google.com'
}, {
id: 4,
name: 'Alice Brown',
address: {
street: '1012 Pine St',
city: 'Springfield',
state: 'IL',
zip: '62701'
},
phone: '555-555-5555',
email: 'alice.brown@google.com'
}];
const compressedData = compress(data);
The compress
function will return an object with two properties: struct
and data
. The struct
property contains the structure of the original JSON data, and the data
property contains the transformed data.
{
"struct": [
{
"id": "n",
"name": "s",
"address": { "street": "s", "city": "s", "state": "s", "zip": "s" },
"phone": "s",
"email": "s"
}
],
"data": [
[
1,
"John Doe",
["123 Main St", "Springfield", "IL", "62701"],
"555-555-5555",
"john.doe@google.com"
],
[
2,
"Jane Smith",
["456 Elm St", "Springfield", "IL", "62701"],
"555-555-5555",
"jane.smith@google.com"
],
[
3,
"Bob Johnson",
["789 Oak St", "Springfield", "IL", "62701"],
"555-555-5555",
"bob.johnson@google.com"
],
[
4,
"Alice Brown",
["1012 Pine St", "Springfield", "IL", "62701"],
"555-555-5555",
"alice.brown@google.com"
]
]
}
In this example, the original size of the JSON data is 668 bytes, and the compressed size is 520 bytes, which is a 22% reduction in size. The actual reduction is even more pronounced in larger datasets.
Note: When the data is very small or consists of non-repeating structures, the compressed data may be larger than the original data because of the compressed data will preserve the structure of the original data. See the examples below.
import { decompress } from 'api-data-compressor';
const originalData = decompress(compressedData);
The decompress
function will return the original JSON data from the compressed data returned by the compress
function.
Case | Original Data | Compressed Data | Compression Ratio |
---|---|---|---|
String |
"Hello, World!" |
{ "struct": "s", "data": "Hello, World!" } |
Original: 15 bytes Compressed: 37 bytes Ratio: 246.67% |
An array |
[ 1, 2, 3, 4, 5] |
{ "struct": ["n"], "data": [1, 2, 3, 4, 5] } |
Original: 11 bytes Compressed: 35 bytes Ratio: 218.18% |
An object |
{ "id": 1, "name": "John Doe" } |
{
"struct": { "id": "n", "name": "s" },
"data": [1, "John Doe"]
} |
Original: 26 bytes Compressed: 54 bytes Ratio: 207.69% |
Complex object |
{
"id": 1,
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "Springfield",
"state": "IL",
"zip": "62701"
},
"phone": "555-555-5555",
"email": "john.doe@gmail.com"
} |
{
"struct": {
"id": "n",
"name": "s",
"address": {
"street": "s", "city": "s",
"state": "s", "zip": "s"
},
"phone": "s",
"email": "s"
},
"data": [
1,
"John Doe",
["123 Main St", "Springfield", "IL", "62701"],
"555-555-5555",
"john.doe@gmail.com"
]
} |
Original: 161 bytes Compressed: 215 bytes Ratio: 133.54% |
Array of objects |
[
{ "id": 1, "name": "John Doe" },
{ "id": 2, "name": "Jane Smith" },
{ "id": 3, "name": "Bob Johnson" },
{ "id": 4, "name": "Alice Brown" },
{ "id": 5, "name": "Charlie Davis" },
{ "id": 6, "name": "Eve Wilson" },
{ "id": 7, "name": "Grace Lee" },
{ "id": 8, "name": "Henry Young" },
{ "id": 9, "name": "Ivy King" },
{ "id": 10, "name": "Jack Wright" },
{ "id": 11, "name": "Kelly Lopez" },
{ "id": 12, "name": "Morgan Hill" },
{ "id": 13, "name": "Nora Green" },
{ "id": 14, "name": "Oscar Adams" },
{ "id": 15, "name": "Penny Baker" },
{ "id": 16, "name": "Quinn Carter" },
{ "id": 17, "name": "Riley Clark" },
{ "id": 18, "name": "Sammy Davis" },
{ "id": 19, "name": "Terry Evans" },
{ "id": 20, "name": "Ursula Fisher" },
{ "id": 21, "name": "Vivian Gray" },
{ "id": 22, "name": "Walter Harris" },
{ "id": 23, "name": "Xavier Irwin" },
{ "id": 24, "name": "Yvonne Johnson" },
{ "id": 25, "name": "Zachary King" },
{ "id": 26, "name": "Amanda Lee" },
{ "id": 27, "name": "Brian Miller" },
{ "id": 28, "name": "Cindy Nelson" },
{ "id": 29, "name": "David Olson" },
{ "id": 30, "name": "Ella Peterson" },
] |
{
"struct": [{ "id": "n", "name": "s" }],
"data": [
[1, "John Doe"],
[2, "Jane Smith"],
[3, "Bob Johnson"],
[4, "Alice Brown"],
[5, "Charlie Davis"],
[6, "Eve Wilson"],
[7, "Grace Lee"],
[8, "Henry Young"],
[9, "Ivy King"],
[10, "Jack Wright"],
[11, "Kelly Lopez"],
[12, "Morgan Hill"],
[13, "Nora Green"],
[14, "Oscar Adams"],
[15, "Penny Baker"],
[16, "Quinn Carter"],
[17, "Riley Clark"],
[18, "Sammy Davis"],
[19, "Terry Evans"],
[20, "Ursula Fisher"],
[21, "Vivian Gray"],
[22, "Walter Harris"],
[23, "Xavier Irwin"],
[24, "Yvonne Johnson"],
[25, "Zachary King"],
[26, "Amanda Lee"],
[27, "Brian Miller"],
[28, "Cindy Nelson"],
[29, "David Olson"],
[30, "Ella Peterson"]
]
}
|
Original: 926 bytes Compressed: 608 bytes Ratio: 65.66% |
Array of objects with different structures |
[
{ "a": 1, "b": 2 },
{ "b": 3, "c": 4 },
{ "c": 5, "d": 6 },
{ "d": 6, "b": 3 },
{ "c": 4, "a": 1 },
] |
{
"struct": [{
"a": "n", "b": "n",
"c": "n", "d": "n"
}],
"data": [
[1, 2],
[null, 3, 4],
[null, null, 5, 6],
[null, 3, null, 6],
[1, null, 4]
]
} |
Original: 71 bytes Compressed: 115 bytes Ratio: 161.97% |
Array with nested objects |
[
1,
{"foo":"bar"},
[2, [{"foo":"bar"}, 4], 5],
6,
[{"foo":[{"a": 1, "b": 2}, [1, 2, 3]]}]
] |
{
"struct": [
{
"foo": "s",
"": {
"": { "foo": "s" },
"foo": [ { "a": "n", "b": "n", "": "n" } ]
}
}
],
"data": [
1,
[ "bar" ],
[ {}, 2, [ [ "bar" ], 4 ], 5 ],
6,
[
{},
[ {}, [ [ 1, 2 ], [ {}, 1, 2, 3 ] ] ]
]
]
} |
Original: 77 bytes Compressed: 136 bytes Ratio: 176.62% |
MIT