views:

434

answers:

8

I'm looking to persist and retrieve a large amount of key-value pair(s) type data over the wire. Is it appropriate for me to use JSON for this purpose against XML?

Is JSON used in non-Javascript applications?

Are there advantages using JSON for this purpose over good old XML?

+14  A: 

JSON is fine for this use, and will be more compact than XML. (We're using it extensively in a non-Javascript application, and allow consumers of our REST web services to specify if they want the data in XML, JSON, or even XHTML representations.)

Update: Where bandwidth is a concern, you can probably get a 50-70% compression by GZIP-encoding/decoding your XML or JSON. See GZipStream.

Jim Ferrans
Just to be clear, JSON and XML have their own strengths and weaknesses. Things like XSL are really cool, but a lot of work to get up to. They are not "the same thing" (I'm just taking this stop to be vocal, I know a lot of people don't get what XML does and say 'hey JSON is easier')
+4  A: 

I find JSON less verbose than XML and there is a very nice database in development called CouchDB which lets you store/search JSON documents directly.

whatnick
+2  A: 

Of course, in fact JSON is much better than XML, for the following reasons:

  • APIs for reading/writing are much (much much) simpler.
    • Retrieving values is as simple as reading keys from a dictionary.
  • It's less verbose, more easily readable by mere mortals.
    • Which also means that huge data take less space and less time to parse.
hasen j
In general people should use JSON and XML packages for reading (parsing) and writing. Newer programming languages have built-in support (see @Michael Dillon).
Jim Ferrans
I never intended to imply that one should write his own json parser, although that's not a very bad idea.
hasen j
JSON is not always better than XML, depending on what you need it for. If I want to do more processing on the file, XML is better, but if I am done with processing, and preparing to display it, then JSON is great.
James Black
XML and JSON are not the same thing stop treating them like they are. JSON is very light weight and great for organizing your data, but it doesn't have any of the built in standards to support it which are very useful in many many situations.
useful in many situations? no, it's only useful in a legacy app that uses xml extensively.
hasen j
+6  A: 

Because JSON is widely used by web application servers to communicate to Javascript in the browser (AJAX) there are lots of JSON encode/decode libraries for every language that you can imagine. For instance Python has 6 different implementations to choose from.

The main advantage to using JSON for a "large amount" of data is that the parsing time to decode the data is quite a bit less than parsing XML. Now it is possible that your key-value pairs are simple enough for parsing time to not make a difference, but if the values have any kind of composite objects in them, then JSON will be faster.

Michael Dillon
A: 

IMO, if you are going to persist things and want to have the possibility to upgrade the application (which is virtually always the case), you should create your serialization code manually, and create a documented file format. Otherwise you're up for big trouble at some point.

erikkallen
+2  A: 

I agree with the others regarding JSON's usability, but with one caveat: JSON does not allow you to provide additional information about the data. If all you want to use is name-value pairs, where all values are strings, JSON is fine.

OK, somebody has probably just voted me down, because the JSON spec differentiates between strings, numbers, booleans, and arrays. But my response to that is: what sort of a number are you getting? A 32-bit integer or a thousand-digit floating-point?

XML, by comparison, allows you to store metadata in attributes. For example, the xsi:type attribute, as defined by XML Schema. Both side still have to agree on what the attributes mean, of course, but there are a lot of cases where metadata is important.

kdgregory
You can store metadata in JSON too. Just as there is an XML Schema, there's a [JSON Schema](http://json-schema.org/). Also, numbers in JavaScript as 64-bit signed doubles.
Eli Grey
@Elijah - (1) A schema -- either XML or JSON -- is external to the data, so you're not providing metadata *in* the JSON. (2) The JSON spec says absolutely nothing about the range of a numeric value, and the OP asked about uses outside JavaScript, so your point about JavaScript numbers is moot.
kdgregory
XSL-FO. That is all.
@user257493 - huh? how does a transformation *from* XML even remotely apply to either the OP's question or my response?
kdgregory
+1  A: 

Whether it is best for you will depend on your priorities, such as, speed, or flexibility.

I had moved a C# application to use JSON because speed was the most important issue for me. I would serialize the data myself on the server and remove anything redundant, such as the name of each property in order to speed up my transfer, and I found that time to send a request to the server, get the response and process was faster with JSON than using a webservice or returning XML.

To deserialize, from C#, you have several options at http://json.org.

So, before you decide to make the change you should have some unit tests and then get some numbers, by making the same client -> server -> client call about 100 times, to get better results.

You should have all the tests in the same test class so the tests can run straight through, to reduce the chance that the server load will issues.

If you will need the flexibility of sorting or processing in other ways, such as using XML LINQ, then you either convert the information to a list and use LINQ, but, again, you may want to add tests to see what the impact of this would be, on your application.

Basically, I believe that if you have time, before making any architectural changes test first, and then decide if making the change makes sense, based on the numbers.

James Black
A: 

It is more compact than XML, and faster to parse in some cases. So I normally prefer it.

On the other hand, XML does solve a few spec problems by providing solutions (f.e. encodings).

However, I can just write in my spec "You must only use UTF-8" and then the encoding specifications problem is solved.

Basically JSON can be used if you need a simple key/value or nested key/value/list structure, and want the common problems of escaping and delimiting solved for you, with many parsers available.

I wrote at least one protocol using JSON, and the users of it never complained about using JSON instead of XML, despite being on Microsoft.NET :)

MarkR