0

I use some rather large JSON files (really, only several hundred "records"). Is compressing said files (using the YUI Compressor) worth the hassle/time?

I get the files via $.getJSON()

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    In the past I have ran into issues with YUI compressor choking on JSON files because they are not valid JS so I would say, No it is not worth it and probably won't work anyway. – marteljn Jul 25 '13 at 20:32
  • Hm...if it's only JSON, you might actually get 99% of YUI's benefit just by searching for and removing all whitespace. But yeah, I don't see too much benefit especially if they're gzip-transferred as Michael mentioned. EDIT: On second thought, such a tool would somehow need to ensure it only removes whitespace outside of strings, which may be more difficult than you'd think. – Katana314 Jul 25 '13 at 21:12

1 Answers1

2

In most cases, it doesn't seem that it would make much sense to do that. How could the YUI compressor offer any benefit on a JSON file without breaking the code that uses that JSON data?

In our discussion in the comments, you mentioned removing whitespace from the JSON file. If you have a hand-edited JSON file with indentation and whitespace and all that, then the compressor could remove that. But most JSON is machine-generated with a call to a function like to_json() or json.dumps() or whatever your server language uses. Typically, unless you specify an option that says you want pretty-printed output, the JSON will be generated without extra whitespace.

Don't forget that your browser and server are almost certainly providing gzip compression (but double-check to make sure that's working properly). To find out the actual size of the data transferred over the wire, gzip your JSON file and see how small it is in that form. Either use the gzip command on Linux or OS X, or on Windows you can use 7-Zip to create a .gz file from your JSON.

I think you may be pleasantly surprised by how small the gzipped version is for a typical JSON file. A lot of the bulk in a JSON file tends to be the repeated property names, and those compress right out.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • To your first paragraph, I was thinking: the same benefit minified *.css and *.js files get when the whitespace is stripped out. To the rest: do you mean this may be happening already behind the scenes, or that my browser and server tolerate my gzipping the json files? – B. Clay Shannon-B. Crow Raven Jul 25 '13 at 21:05
  • Is your JSON file hand-coded, or is it generated by a `to_json` or similar command in your server language? If it's generated by a JSON library, it shouldn't have any extra whitespace in it unless you request it with an option when you call the JSON library. The point about `gzip` is that your server and browser should be doing this automatically. All of your HTML, CSS, JS, JSON, and all other data should be getting gzipped by your server. My suggestion of running `gzip` manually is simply so that you can see the actual size of the data transferred across the wire. – Michael Geary Jul 25 '13 at 21:08
  • Okay, that's good (that they're probably doing it manually). The way I get my json to be json is a bit weird, perhaps: 0) I compile a text file, bit by bit 1) I send that through a C# utility I wrote that converts the file into a csv representation of the data therein 2) I convert it to json via an online csv to json converter 3) I czech the resulting json using an online jsonlint utility 4) I save the resulting massaged text to a notepad file, and rename the extension to .json 5) I add the json file to my asp.net project. 6) I retrieve the file using $.getJSON(). – B. Clay Shannon-B. Crow Raven Jul 25 '13 at 22:16