Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gzipped JSON file not decompressing

Tags:

I have large (30MB+) JSON files stored on my server as file.json. Using jQuery's getJSON("http://site/file.json") function it works just fine. But, as you'd probably think downloading 30MB worth of a JSON response takes a decent chuck of time.

Instead I'm now storing them as Gzip'd files (file.json.gz) which cuts them down to just under 1MB! I'd like to do the same thing by using getJSON("http://site/file.json.gz"), but it looks like browsers are not decompressing the GZIP'd response, so parsing it doesn't work.

So, is there any way to get jQuery or the browser to decompress a static JSON file that's been compressed using GZIP such as file.json.gz?

BTW: The saved files are compressed properly. If I manually decompress them I get the 30MB+ valid JSON file.

like image 354
JacobFischer Avatar asked Dec 02 '11 01:12

JacobFischer


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 much can gzip compress JSON?

Gzip has a high compression ratio around 95% with CSV and JSON.

What program opens a GZ file?

The most commonly used programs to open GZ files are WinZip or the native GZIP software for Unix and Linux users.

How do you check if files are Gzipped?

gzip compressed files often have the . gz file extension (in fact, I don't think I've ever seen a . gzip extension), but it's generally unsafe to rely on file extension to test for the type of file anyhow. The c 'library' gzip, ie gzopen/gzread/etc will transparently read uncompressed files.


1 Answers

Browsers don't automatically decompress just any compressed data they run across. The server has to tell the browser that the stream is only compressed for transport and it needs to be decompressed before processing (otherwise you wouldn't be able to download and save compressed archives at all!) You need to make your web server send the appropriate headers (Content-Encoding), but do check that the browser supports compression in the first place before doing that (the Accept-Encoding header). Most webservers can also gzip things for you on the fly, unless that's too big of a performance hit.

Somehow I have the feeling you're going to have more trouble parsing that much JSON on the browser than retrieving it...

like image 76
Matti Virkkunen Avatar answered Sep 27 '22 03:09

Matti Virkkunen