Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing a JSON string? (And strings in general)

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)

like image 545
Shaun Dreclin Avatar asked Jan 17 '16 14:01

Shaun Dreclin


People also ask

Can JSON files be compressed?

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.

How can I convert JSON to string?

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.

Can you minify JSON?

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.

How do I make a JSON file smaller?

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.


1 Answers

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.

Example

(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.

like image 79
Robert Rossmann Avatar answered Oct 17 '22 14:10

Robert Rossmann