When to prefer JSON over XML? When to prefer JSON over XML? xml xml

When to prefer JSON over XML?


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


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.


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 javascriptyou'd have to write less code on theclient side - Just eval() (or, better yet, JSON.parse()) the JSONstring and get an object you canuse.

  • At the same time evaluating JSON onthe client-side will be moreefficient, and therefore faster.

  • JSON serialization produces shorterstrings than XML. Using JSON willreduce the amount of data runningacross the wire and improveperformance in that respect.

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