by Arthur Correa on 6/7/2015
Share on Twitter or Facebook

Recently I was discussing about whether to use JSON or XML as the data exchange format for a web service.  I was surprised to hear that in today's environment my coworker wanted to use XML instead of JSON.  As for myself I pushed for JSON for a number of reasons.

Now back when I first started coding years ago XML was just gaining in popularity.   At the time flexibility provided by the data structure was something new and incredibly useful.  Couple that with its ability to have defined schemas and transformations and it was a very interesting and powerful tool to use.  The first time I implemented web services in production I used XML for the requests and responses, and it worked very well.  After that I moved on and worked some SOAP services and again the XML worked really well.

Then about a decade or so ago I was introduced to the idea of using JSON instead of XML.  I admit, at first I was resistant.  Why change?  XML worked well, and met all of the needs so why bother?  However I gave it a shot.  It worked ok, but I still didn't quite get what the big deal was.  Why was JSON better than XML?

Since then I've done a few more projects that used JSON over XML and I learned a few more things about the beneifits of JSON.  

First JSON is less verbose, its not an order of magnitude less, but it is definitely less.  This means to send the same amount of information to your client, you have less structure defining elements around it, making the overall transfer smaller.  This was evident to me from the get go, but I didn't quite place enough weight on this fact at first.  I thought the benfits of XML made the extra data worthwhile.  Over time I came to realize that those benefits of XML really weren't as important to me as the smaller size.

Second, parsing is generally faster with JSON.  The parser doesn't have to do as much work as the nature of the JSON format maps to an object whereas in XML the data structure is meant to have the ability to be independant of anything else and as a result can take more work to map to/from an object instance.

Finally the fact that it works so well with the modern client side javascript libraries today make the decision to use JSON almost a no brainer nowadays.  I've written sites that use client libraries such as Angular and React/Reflux and having my web service work with JSON rather than XML made using those libraries incredibly easy.  For this reason alone I'd say just stick with JSON.  

Now, those technical reasons aside, ultimately it comes down to how your clients want to consume your data.  If they want XML then use XML or they won't use your web service.  If they want JSON then return JSON.  More often than not you'll want to return both, and the way Java and C# work, you actually don't have to make the decision, its all decided by the client's request and the server knows how to return the right response time.  However the majority of clients I've seen recently just want to work with JSON, so make your customers/clients happy and do what they want.  Now this isn't to say that XML doesn't have a place.  I think there are very few hard and fast rules when designing systems, there is a time and place for everything.  But for today whenever I build a web serivce my default is to say its going to be using JSON.

by {{ comment.AuthorName }} on {{ comment.DefaultDateStringFormat }}
{{ comment.Text }}

* This is not a valid email.
This is not a valid url.