When to prefer JSON over XML?

Viewed 39148

My requirement is just to display a set of values retrieved from database on a spread. I am using jquery.

19 Answers

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.

Usually JSON is more compact, and faster to parse.

Prefer XML if:

  • You need to process the data on the client, and you can leverage XSL for that. Chances are the XML + XSL chain will work faster than JSON + JavaScript especially for big chunks of data.
    • One good case is to convert the data into an HTML snippet.
  • Various legacy cases:
    • There is an existing XML service, and it is a hassle to rewrite it with JSON for some reasons.
    • You have to post this data back as XML after some light processing using user's input.

One important case of (almost) XML: try to detect when sending HTML snippets is more beneficial than sending raw data. AHAH can do wonders in simple applications, yet frequently overlooked. Usually this style assumes that a server sends HTML snippets that will be inlined in the web page without processing.

Usually in AHAH cases CSS is being leveraged to the max to massage snippets visually and implementing simple conditionals like hiding/showing relevant parts of the snippet using user-specific or application-specific settings.

JSON is always preferable in terms of the processing the client browser has to do for parsing the data. Also, JSON is light weight data exchange format.

XML parsing always consumes lot of browser resources and should be avoided as much as we can unless otherwise required.

JSON is easy and faster to parse. XML is a little more difficult to parse, and is slower to parse and transfer (in most cases).

Since you're using jQuery, I suggest using JSON: jQuery can retreive JSON data and convert it into a Javascript object automatically. In fact, you can convert JSON data into a Javascript object using eval. XML would have to be transversed manually by you (I don't know how this works in Javascript, but it's difficult/more annoying in most languages I've used XML libraries with).

I'd choose XML over JSON if I need to validate the chunk of incoming data, because XML nativly supports this through XSD.

JSON is the native encoding for javascript. It should be much faster and easier to work with.

I use JSON for any kind of configuration, data interchange or messaging. I use XML only if I have to for other reasons or to semantically mark up document-like data.

Both XML and JSON are supported by Microsoft. XML literals were the new cool feature in VB 9. In the upcoming version of ASP.NET 4.0 JSON is a must to leverage the power of client side templating.

From the question you have asked it seems JSON might be the choice for you as it is easy to process on client side with or without jQuery.

I am seeing a bit of bias dogma here. It appears that answers for this are over simplified for xml and coming from the context of Web Development only (which makes sense for the question), so I figured id offer some additional insight just in case some one crosses this and needs an answer for data serialization in other contexts.

Here are the hard and fast rules:

XML is definitely, not arguably, more powerful. So use it when your data model is complicated enough to need the following features:

  1. Support for Namespaces
  2. Support for Object Orientation ie inheritance/polymorphism
  3. Support for inclusion and extensibility for complex type reuse.
  4. Support needed for a reliable, mature and complete Schema Validation system.
    • the w3c Schema Validation system is vastly more capable to JSON Schema and has more literature to learn it.
  5. Support for mixed content document data modelling up AND record like data modelling.

JSON is simpler to learn, understand and use. So use it when you dont have time to learn XML and dont have a need for any of the above features. Its also more lightweight on the wire, if that matters for your use case.

TL:DR, XML can do everything json can do, but is heavier. The reverse is simply not true. Yes Json is simpler and therefor used more, but that does not mean it can supplant XML. In the case I was up against this year, 2020, json didnt make the cut for our use case, we literally needed XML. I can talk about that more if needed. Cheers and good luck.

Related