When to prefer JSON over XML?

Considering your specific case where you're already doing javascript on the client side, I'd go with JSON for these reasons:

  • Since JSON is native to javascript you'd have to write less code on the client side - Just eval() (or, better yet, JSON.parse()) the JSON string and get an object you can use.

  • At the same time evaluating JSON on the client-side will be more efficient, and therefore faster.

  • JSON serialization produces shorter strings than XML. Using JSON will reduce the amount of data running across the wire and improve performance in that respect.

Here's some further reading: http://www.subbu.org/blog/2006/08/json-vs-xml


I use JSON unless I'm required to use XML. It's simpler to understand, and (because it requires less configuration overhead) it's easier to program for reading and writing if the libraries are available in your context, and they're pretty ubiquitous now.

When Amazon first exposed their catalogs as a web service, they offered both JSON and XML. Something like 90% of the implementers chose JSON.


Some other things that I have run into in the XML vs JSON relm:

JSON is very good for

  • name/value pairs
  • nesting those pairs

Which means it tends to like an array or nested array. However JSON is missing both

  • attributes
  • namespacing

So if you were to combine two or more JSON services there could be potential namespace conflicts. That being said JSON can be used for about 90% of the same things XML can be used for when exchanging data in my experience.


Favor XML over JSON when any of these is true:

  • You need message validation
  • You're using XSLT
  • Your messages include a lot of marked-up text
  • You need to interoperate with environments that don't support JSON

Favor JSON over XML when all of these are true:

  • Messages don't need to be validated, or validating their deserialization is simple
  • You're not transforming messages, or transforming their deserialization is simple
  • Your messages are mostly data, not marked-up text
  • The messaging endpoints have good JSON tools