20

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.

JacobFischer
  • 693
  • 1
  • 8
  • 15
  • 2
    What you *should* do is configure your web server to gzip your JSON responses with appropriate encoding. Then the server will handle compression and the web browser will handle decompression completely under the covers transparent to your code. I'm not posting this as an answer as I don't know your circumstances. But that's how you should do it. – Strelok Dec 02 '11 at 01:47
  • 2
    It is also possible to precompress your JSON and then just tell Apache to serve the files with the appropriate headers to avoid having to recompress all the time. – Michael Mior Dec 02 '11 at 01:50

3 Answers3

11

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

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

You would not have to manually gzip the file on server. If the browser and hence underlying XMLHttpObject supports Accept-Encoding gzip, deflate header then the file (response) will be zipped (deflated) by server before transmission.

However you need to configure your webserver (if using IIS, please check its metabase.xml) to apply deflate on response, as per incoming HTTP request's header.

Configuration Help:

For Apache web server refer : http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

For IIS : http://www.codinghorror.com/blog/2004/08/http-compression-and-iis-6-0.html

jatanp
  • 3,982
  • 4
  • 40
  • 46
1

Although there are arguments against doing so, one can decompress gzipped data with JavaScript. See this question and this question for solutions.

Community
  • 1
  • 1
Jakob
  • 3,570
  • 3
  • 36
  • 49