I have an application that performs a little slow over the internet due to bandwidth reasons. I have enabled GZip which has improved download time by a significant amout, but I was also considering whether or not I could switch from XML to JSON in order to squeeze out that last bit of performance. Would using JSON make the message size significantly smaller, or just somewhat smaller? Let's say we're talking about 250kB of XML data (which compresses to 30kB).
views:
186answers:
4Yes JSON would be about 30% faster there are fewer characters going over the line and its very quick to parse.
Ypu could also take a look at "YAML" which sends an absolute mininmum of metadata in the message.
Generally speaking, JSON is much faster and smaller than the equivalent XML. XML is richer in that you can store metadata (attributes) and content separately, but the same can be achieved in JSON with appropriate structuring conventions.
Not an answer, but rather a suggestion to examine your assumptions.
How is JSON smaller?
JSON:
"person":{"firstname":"Fred",
"lastname":"Flintstone",
"age":38,
"spouse":"Wilma" }
XML:
<person firstname='Fred'
lastname='Flintstone'
age='38'
spouse='Wilma'/>
I just don't see how, in general, a JSON expression is going to be 30% smaller than "equivalent" XML. Even as you ramp up the complexity of these things, with nested structures and arrays, it's not going to be a 30% difference. It's approximately equivalent, with json earning an advantage because the end tag of a nested structure is a } , while XML earns an advantage because it need not quote field names.
If you forced me to use XML elements, like this:
<person>
<firstname>Fred<firstname>
<lastname>Flintstone<lastname>
<age>38</age>
<spouse>Wilma</spouse>
</person>
...sure, the resulting XML is larger than the prior JSON. But that seems like cheating.
Now it may be that the way you format your XML currently uses elements for everything, and that there's an opportunity to shrink the payload accordingly. But that doesn't necessarily imply JSON. If you've got tools and libraries that handle XML, you can keep XML and shrink.
The best way to answer this is to test it yourself, since compression is involved. You also neatly avoid the XML vs JSON holy war by having an objective answer!
Since it's just a test and doesn't really need to work, you could just write up an xml->json converter in javascript that walked the XML DOM tree and copied it into a nested array/object structure then passed it to JSON.stringify(). The only tricky bit would be deciding what becomes an array and what becomes an object.
Find a representative sample (or a few) of the data being sent, convert it to JSON with your tool, gzip it and the original XML and compare sizes.
Note: I looked around for an online XML->JSON converter and they were all terrible--copius whitespace outside of quotes (illegal in JSON, alters size) and unquoted key names (ditto). Don't use them for your test or you'll get bad data.