anu
anu

Reputation: 1007

How to get javascript to parse json.gz file on browser

In a nutshell - Essentially I am trying to create a Heavy-client & Light-Server Single Page application.

I am GET-ing a compressed JSON data file via a simple HTML script tag.

<script type="text/javascript" src="myjson.json.gz"></script>
<script type="text/javascript" src="parse.js"></script>

myjson.json file has content similar to:

media_library={some:"json", data: "here"}

Only, it's about 6MB. The parse.js javascript code has logic to manipulate the create the web-page using the data from media_libary variable declared in myjson.json file.

Compressing it to myjson.json.gz reduced the size to 650KB. Which allows for a faster load-time.

I googled for ways to either:

(1) Find code to get javascript to inflate the gzip file to myjson.json and then get it going

(2) Find code to get javascript to read the json object from the gzip file

I could not find anything on either.

I m looking for a way to save network time for transferring the entire data.

The idea of getting the server to send JSON that's only required instead has crossed my mind, but that goes against my intention of keeping the server light and making my client do the searching,sorting,filtering and presenting (heavy client stuff).

Is there a way I can get the server to just send the JSON data compressed only to decompress and work on in on the browser ?

Thanks.

I am using python -m CGIHTTPServer as my server.

This is the browser's network activity inspector.

network activity

Upvotes: 4

Views: 5266

Answers (1)

Brad
Brad

Reputation: 163438

Use a compressed transfer encoding, like gzip. Chances are your server is already configured to serve this, and your browser can also already handle it. Then, you don't need to do anything. As a bonus, the browser can specify what encodings it supports.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding

The specifics of implementing depend on your server. Usually it's a simple config line. If you have a resource that is hit very frequently, you can also pre-compress it so the server just has to send it.

Upvotes: 2

Related Questions