Thursday March 01, 2007
Language-neutral data format: XML and JSON
XML and JSON are the two prevalent choices for language-neutral data format. That means a format used to exchange data between client and server, independent of the language used on each end. We are familiar with XML pointy bracket syntax which has served us well so far. With Rich Internet Applications becoming more common, there is a need to have a light-weight data interchange format. And so JSON is catching up (11% for data transfer in 2006).
Basically, JSON is built on two structures:
See message samples formatted in JSON and equivalent XML. Tim Bray summarizes when to use which format.
Here is a collection of interesting articles in case you want to dig deeper:
The key advantages of JSON I derived from my reading of the above articles are:
There are some disadvantages as well:
}}]} at the end of data snippet is frightening
and debugging pain.In jMaki, we use JSON in Java. Here is a sample code to create a JSON object using these APIs:
import org.json.*;
import java.io.*;
public class JSONSample {
public static void main(String[] args) throws Exception {
// basic object creation
JSONObject person = new JSONObject();
person.put("name", "duke");
person.put("age", "10");
System.out.println(person.toString());
// how to create array and write to a "writer"
JSONObject address = new JSONObject();
JSONArray array = new JSONArray();
array.put("4140, Network Circle"
;
array.put("Santa Clara"
;
array.put("CA - 95054"
;
address.append("address", array);
OutputStreamWriter osw = new OutputStreamWriter(System.out);
address.write(osw);
osw.flush();
// XML->JSON conversion
JSONObject likes = XML.toJSONObject("<likes><running/><skiing/></likes>");
System.out.println(likes.toString());
}
}
And here is the corresponding output:
{"age":"10","name":"duke"}
{"address":[["4140, network circle","Santa Clara","CA - 95054"]]}
{"likes":{"skiing":{},"running":{}}}
This API also allows conversion from comma-delimited text, HTTP, Cookie, and CookieList to JSON conversions. The source code for JSON in Java is freely available but here are two suggestions for ease-of-use:
In summary, XML is document-oriented and JSON is data-oriented. So if you want to deal with highly structured documents that requires a complex structure, binary data, exact ordering of elements and be able to render itself then use XML. OTOH, if you are focused on light-weight data exchange then JSON is the way to go.
Follow the JSON blog and enjoy!
Technorati: XML JSON DataFormat JavaScript Web2.0 Ajax jMaki
Posted by Arun Gupta in web2.0 | Comments[4]
|
|
|
|
| « October 2008 | ||||||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
2 | 4 | |||||
5 | 6 | 7 | 8 | 9 | 11 | |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 | |
| Today | ||||||
Posted by niraj on March 28, 2007 at 08:59 AM PDT #
Posted by Arun Gupta's Blog on April 20, 2007 at 06:32 AM PDT #
Hi Arun,
just tried the http://json-taglib.sourceforge.net/
to produce json for a jmaki widget. But the json format of the json-taglib seems to be incompatible withe the jmaki json format.
Any Hints?
See also:
http://groups.google.com/group/json-taglib/browse_thread/thread/516011f4ca9f831b
Thanks, Thorleif
Posted by Thorleif Wiik on September 30, 2007 at 01:44 PM PDT #
This is already answered at:
http://groups.google.com/group/json-taglib/browse_thread/thread/516011f4ca9f831b
Posted by Arun Gupta on October 03, 2007 at 07:21 AM PDT #