Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "compressed JSON"?

I see a lot of references to "compressed JSON" when it comes to different serialization formats. What exactly is it? Is it just gzipped JSON or something else?

like image 282
ibz Avatar asked Nov 17 '10 04:11

ibz


People also ask

What is JSON compression?

Store JSON data in space efficient manner. Inspired by compressed-json and jsonpack. This library is optimized to reduce represent json object in compact format, which can save network bandwidth and disk space. It is not optimized for writing nor querying throughput.

Are JSON files 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.


1 Answers

Compressed JSON removes the key:value pair of json's encoding to store keys and values in seperate parallel arrays:

// uncompressed
JSON = {
  data : [
     { field1 : 'data1', field2 : 'data2', field3 : 'data3' },
     { field1 : 'data4', field2 : 'data5', field3 : 'data6' },
     .....
  ]
};
 
//compressed
JSON = {
    data : [ 'data1','data2','data3','data4','data5','data6' ],
    keys : [ 'field1', 'field2', 'field3' ]
};

This method of usage i found here

Content from link (http://www.nwhite.net/?p=242)

rarely find myself in a place where I am writing javascript applications that use AJAX in its pure form. I have long abandoned the ‘X’ and replaced it with ‘J’ (JSON). When working with Javascript, it just makes sense to return JSON. Smaller footprint, easier parsing and an easier structure are all advantages I have gained since using JSON.

In a recent project I found myself unhappy with the large size of my result sets. The data I was returning was tabular data, in the form of objects for each row. I was returning a result set of 50, with 19 fields each. What I realized is if I augment my result set I could get a form of compression.

// uncompressed

JSON = {
  data : [
     { field1 : 'data1', field2 : 'data2', field3 : 'data3' },
     { field1 : 'data4', field2 : 'data5', field3 : 'data6' },
     .....
  ]
};

//compressed

JSON = {
    data : [ 'data1','data2','data3','data4','data5','data6' ],
    keys : [ 'field1', 'field2', 'field3' ]
};

I merged all my values into a single array and store all my fields in a separate array. Returning a key value pair for each result cost me 8800 byte (8.6kb). Ripping the fields out and putting them in a separate array cost me 186 bytes. Total savings 8.4kb.

Now I have a much more compressed JSON file, but the structure is different and now harder to work with. So I implement a solution in Mootools to make the decompression transparent.

Request.JSON.extend({
 
    options : {
        inflate : []
    }
 
});




Request.JSON.implement({
 
    success : function(text){
        this.response.json = JSON.decode(text, this.options.secure);
        if(this.options.inflate.length){
            this.options.inflate.each(function(rule){
                var ret = ($defined(rule.store)) ? this.response.json[rule.store] : this.response.json[rule.data];
                ret = this.expandData(this.response.json[rule.data], this.response.json[rule.keys]);
            },this);
        }
        this.onSuccess(this.response.json, text);
    },
 
    expandData : function(data,keys){
        var arr = [];
        var len = data.length; var klen = keys.length;
        var start = 0; var stop = klen;
        while(stop < len){
            arr.push( data.slice(start,stop).associate(keys) );
            start = stop; stop += klen;
        }
        return arr;
    }
 
});

Request.JSON now has an inflate option. You can inflate multiple segments of your JSON object if you so desire.

Usage:

new Request.JSON({
       url : 'url',
       inflate : [{ 'keys' : 'fields', 'data' : 'data' }]
       onComplete : function(json){}
});

Pass as many inflate objects as you like to the option inflate array. It has an optional property called ’store’ If set the inflated data set will be stored in that key instead.

The ‘keys’ and ‘fields’ expect strings to match a location in the root of your JSON object.

like image 111
ArK Avatar answered Oct 04 '22 02:10

ArK