json1

Compress JSON data

JSON is good data format to grab some server side data through an AJAX call. For example by using jQuery.ajax method. In PHP, to provide data in JSON format in super simple. However, when the data is large, do you know any ways to compress the data to facilitate for fast transmission.

JSON – JavaScript Object Notation

Here is a simple tutorial to introduce how to use format data in JSON format in PHP.

First, you need to set the content-type to application/json and encode your data using json_encode function and output it.

<?phpheader('content-type: application/json; charset=utf-8');
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
echo json_encode($data);?>

By using FireBug console, you can test it by running this line as below:

$.ajax({url: 'data.php'})
// Response: [1,2,3,4,5,6,7,8,9]

And you should see the request successfully executed and result as above. If you cannot make it work, you may be subject to the following:

Due to browser security restrictions, most “Ajax” requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
Simple compression of JSON data

Here it is going to demonistrate using gz compression on JSON data.

All you need is regular output buffering with the ob_gzhandler as output callback.

// Fetch some data
$data = get_data();</div>
// Turn on output buffering with the gzhandler
ob_start('ob_gzhandler');
<div>// Output as normal
echo json_encode($data);

The ob_gzhandler() method do thing smartly, before it sends compressed data, it determines what type of content encoding the browsesr will accept (Like: “gzip” or “deflate” or none) and will return its output accordingly. Most of the morden browsers are supported, however it is up to browser to send correct header saying that it accepts compressed web pages.

Tried adding it for a text field with timezone auto-completing for example, and without this handler:

Content-Length    5517
Content-Type      application/json

With this handler:

 Content-Encoding  gzip
 Vary              Accept-Encoding
 Content-Length    1775
 Content-Type      application/json

Related Posts


11 thoughts on “Compress JSON data

  1. kore dizileri izle

    I was curious about your future publish admin genuinely necessary this blog super incredible web site

    Reply
  2. Keira

    Really vital publish admin excellent a person i bookmarked your word wide web webpage see you in up coming weblog submit.

    Reply
  3. Wilber Hertweck

    I simply want to tell you that I’m newbie to blogs and really enjoyed you’re web-site. More than likely I’m want to bookmark your blog . You actually come with great writings. Thanks a bunch for sharing your web-site.

    Reply
  4. Tomasa Barczak

    hello!,I really like your writing so a lot! percentage we keep up a correspondence more about your post on AOL? I require an expert on this space to unravel my problem. Maybe that’s you! Having a look ahead to see you.

    Reply
  5. Harvardbusiness Review

    I was recommended this web site by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!

    Reply
  6. Terry Ovdenk

    I simply want to tell you that I am just beginner to weblog and seriously liked you’re page. More than likely I’m planning to bookmark your website . You amazingly have wonderful articles. Thanks for sharing your website page.

    Reply
  7. SEO for Beginners

    It’s actually a cool and useful piece of information. I’m glad that you simply shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

    Reply
  8. check domain stats

    Useful info. Fortunate me I discovered your web site by accident, and I am surprised why this accident didn’t came about earlier! I bookmarked it.

    Reply
  9. orange messagerie

    Its like you learn my mind! You appear to know so much approximately this, such as you wrote the guide in it or something. I feel that you can do with a few percent to power the message house a bit, however instead of that, that is magnificent blog. A fantastic read. I will definitely be back.

    Reply
  10. hausrat versicherung

    Hello there, I discovered your site by the use of Google whilst searching for a related matter, your website got here up, it appears good. I have bookmarked to my favourites|added to my bookmarks.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*