rickyduck
rickyduck

Reputation: 4084

Data efficiency - return in JSON or XML?

I have fairly large set of data returned via AJAX from a page. This can be anything up to 0-20k records at once, each with around 10 peices of data inside. Now, at the moment, the data is returned in structured XML and the javascript deals with it (for the record, I'm using jQuery at the moment).

When the XML comes back, the jQuery loops through all the nodes called address (using the .find() function). It then sorts the data respectively and can take any amount of time. However it does appear the code isn't as efficient as maybe it could be, and I don't really have any experience handling largish data. I guess the benefit of the data being XML is we can easily integrate it into other things, eg Web services etc... but efficiency would be much better as we dont want to leave the user "hanging". So I'm sure other people may have asked this before, but what's more efficient - XML or JSON?

Also, which one is more useful, or are they both the same?

Sorry if it's a lame question, this is one of the first times I've used JSON!

Upvotes: 4

Views: 3375

Answers (2)

G_H
G_H

Reputation: 11999

JSON is a notation for serializing objects. That's its focus and purpose. XML is for defining markup languages, but I don't like it if people hammer that fact home as if it can't be used for data. I find XML very suitable for representing a multitude of data structures. However, XML is rather general-purpose and that brings some overhead with it in terms of specification, notation and tool size.

First of all, find out exactly what you need. Do you need to transfer data in a more general sense, or is passing around objects sufficient? Will you require things like namespaces? Choose a technology that does what you need, but not more than that. But do keep future expansion in mind.

Secondly, consider the tools. XML has great support in almost any language. There's methods for in-memory representation (DOM), object binding (JAXB in Java), parsing (SAX)... Does JSON have as much support in your target environment? On the other hand, JSON is suppremely convenient on the client-side in combination with JavaScript.

I believe you'll be able to do what you need regardless of technology choice, and within each choice there's room for optimization. But there's one final thing to consider: maybe you don't have to choose. Sometimes it can be really simple to allow data to be serialized as JSON or XML. Being a Java programmer, this is the only example I can come up with, but in JAX-WS there are methods for getting data from webservices as XML, JSON or maybe even other formats with minimal code adaptation.

Upvotes: 7

FloydThreepwood
FloydThreepwood

Reputation: 1585

My personal opinion:

I don't like XML much. But it depends on the depth of your transfered objects. If you do not need a bunch of attributes and tags to represent your data xml is most certainly not the best choice. (Examples and much more reasoning). When I am free to define my own Datastructure and hierarchy JSON is the much quicker way. Also it's syntax isn't nearly as heavy as xml's so you can save some bits or even bytes per transmission.

For me there is only one reason to use XML in modern applications: convenience! When you are applying to open standarts (e.g. using REST with a ATOM Frontend). You see benefits in cooperation with other developers quickly.

In the end it's up to you to decide what your data will look like and then choose the best representation.

Upvotes: 2

Related Questions