Solved

How to gzip JSON from PHP using AJAX?

Posted on 2013-12-09
10
2,238 Views
Last Modified: 2013-12-11
Hi,

I'm wondering how I can gzip json data from PHP with an AJAX call.

I was reading here that in PHP 5.4 you can use:

json_encode($data,JSON_UNESCAPED_UNICODE);

Unfortunately, I don't have PHP 5.4 installed right now.

So the other user suggested:

ob_start('ob_gzhandler');

But that just doesn't work when I place that code at the top of my PHP file. Server returns error.

I'm looking for an alternative to:
json_encode($data,JSON_UNESCAPED_UNICODE);

and also how would I unzip the json data in the $.ajax call:

$.ajax({
    type : 'post',
    url: './model/06_modCheckPrimaryBeneficiaries' + sAppendDateTime + '.php',
    cache: false,
    dataType: 'json',
    success: function(retData, textStatus, jqXHR) {
        var test = retData;
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('textStatus: '+textStatus);
    });

Open in new window

0
Comment
Question by:Victor Kimura
  • 4
  • 3
  • 3
10 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39706636
If you are talking about delivering your JSON in 'gzip' format to the browser, that is not something that you choose or do.  That is a negotiation between the browser and the web server.

In the HTTP request from the browser, the browser tells the server what formats it can accept in the response.  In the HTTP response (when 'gzip' is acceptable), the server checks the file size to determine whether it would be quicker to just send a small file or gzip and send a larger file and then does what seems to be the best choice.
0
 

Author Comment

by:Victor Kimura
ID: 39706662
Oh, okay.

So, this code:
json_encode($data,JSON_UNESCAPED_UNICODE);

isn't really gzipping it then?

Is that true of some .js files too? I have another post here that you may be able to answer too:
http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Apache/Q_28313939.html
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39706688
The 'gzip' method of sending data from the server to the browser is the same for all file types.  You can 'gzip' a file for storage on your server but that is a different thing.
0
 

Author Comment

by:Victor Kimura
ID: 39706746
Ok, thanks.

just wondering if you know if this code:
json_encode($data,JSON_UNESCAPED_UNICODE);

will make the json data smaller or more efficient then just regular encoding.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39708321
If the JSON is so large that making it smaller is a worthwhile objective, the application is misdesigned.  JSON_UNESCAPED_UNICODE is not related to GZIP.  PHP says: "Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0."

The JSON standard requires UTF-8.  PHP has a long history of thinking that a byte == a character, and this is not true in UTF-8, where characters can be made up from multiple bytes.  So my guess is that PHP was encoding the Unicode characters to try to play nice with legacy applications and avoid breaking the JSON standard.

See these links for more info on Unicode:
http://php.net/manual/en/json.constants.php
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Victor Kimura
ID: 39709351
Ok, I see. How big is too big? I have a some form fields that return in JSON format. The user can add several beneficiaries (it's for estate planning) to this particular page and each beneficiary the user adds increases the JSON by about 24 KB. So, if the user adds in 3 beneficiaries, then that JSON return is about 72 KB. Is that too large? I just want to see how I can optimize the JSON and what's considered too large. I'm trying to see how I can make this more efficient.

Thank you for your input.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39709399
72KB is not too large, nor is ten times that.  I was thinking more like the size of the Amazon.com catalog as being too large.

If you're finding timeouts when you submit the form, you might consider a design that used AJAX to submit the form in parts.
0
 

Author Closing Comment

by:Victor Kimura
ID: 39711899
Thanks so much for your valuable input. Both of you! =)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39711959
Glad we could help.  Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39712076
You're welcome, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now