nbolton nbolton - 1 year ago 34
JSON Question

How does JSON compare to XML in terms of file size and serialisation/deserialisation time?

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).


Not an answer, but rather a suggestion to examine your assumptions.

How is JSON smaller?


          "spouse":"Wilma" }


<person firstname='Fred' 

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:


...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.