How should I go about compressing a very large JSON string to transmit over websockets? (Also later to store in localStorage)
It's already minified, but I need something that can do this: http://www.unit-conversion.info/texttools/compress/ (I tried poking about in the source there and couldn't figure it out)
As text data, JSON data compresses nicely. That's why gzip is our first option to reduce the JSON data size. Moreover, it can be automatically applied in HTTP, the common protocol for sending and receiving JSON. Let's take the JSON produced with the default Jackson options and compress it with gzip.
Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
Users can also minify the JSON file by uploading the file. Once you have minified JSON Data. User can download it as a file or save it as a link and Share it. JSON Minifier works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.
Create a serialize/deserialize function which takes a JSON file and creates a compressed binary storing your data as compactly as is reasonable, then read that format on the other end of the line.
You can compress any kind of binary data (strings, Buffers) with Node.js, no external deps required, using the zlib module.
You can use either gzip or deflate compression algorithms, depending on your needs.
(shamelessly stolen from Node.js' website)
const zlib = require('zlib')
const input = JSON.stringify({ some: 'json-data' })
zlib.deflate(input, (err, buffer) => {
if (err) {
console.log('u-oh')
}
// Send buffer as string to client using my imaginary io object
io.send(buffer.toString('base64'))
})
Update: It might be better to just enable HTTP compression on the transport layer instead of compressing and decompressing the data on your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With