Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to prefer JSON over XML?

People also ask

Why do we prefer JSON over XML?

JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.

When would you use JSON vs XML?

JSON is a good choice if you want to transmit data from a client to a server. JSON data is readily processed by most programming languages. This means that you will not have to do any data parsing on either the client or the server side after data has been sent. XML is good if you need to separate data from HTML.

When should JSON be used?

It is a text-based way of representing JavaScript object literals, arrays, and scalar data. JSON is relatively easy to read and write, while also easy for software to parse and generate. It is often used for serializing structured data and exchanging it over a network, typically between a server and web applications.

Which is better format JSON or XML and why?

JSON is quicker to read and write. XML file takes time to read and write because the learning curve is higher. JSON can use arrays to represent the data. XML does not contain the concept of arrays.


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


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.