Storing compressed json data in local storage

As localStorage functionality seems to be limited to handle only string key/value pairs what you can do is to use lz-string library to stringify the json object and compress it before storing it in localStorage

Usage

// sample json object
var jsonobj = {'sample': 'This is supposed to be ling string', 'score': 'another long string which is going to be compressed'}

// compress string before storing in localStorage    
localStorage.setItem('mystring', LZString.compress(JSON.stringify(jsonobj)));

// decompress localStorage item stored
var string = LZString.decompress(localStorage.getItem('mystring'))

// parse it to JSON object
JSON.parse(string);

Check jsfiddle http://jsfiddle.net/raunakkathuria/7PQtC/1/ as i have copied the complete script so look for running code at end of fiddle

Use compressToUTF16 and decompressFromUTF16 if you are dealing with UTF characters but please note that the strings produced are therefore 6.66% bigger than those produced by compress. So use utf ones only when required.

Leave a Comment