Can JSON be friends with XSD?

XML is widely used to represent data. XML is often talked of as a human readable format. Yes it is readable. But how often do we need to do that. Use it for its worth.... data representation.

XML Schema files are used to represent the elements that can be contained in an XML file, including data element names, data structures, types, sequence, etc. Given an XSD file it is easy to use available tools and API's to verify correctness of a document (validity and well form"ness").

JSON (JavaScript Object Notation) is another data representation and exchange format. JSON is receving some attention nowadays due to the AJAX speed train. When you think AJAX there are two types of clients:
  1. AJAX-lite clients. These web pages use XMLHttpRequest or some wrapper framework to make asynchronous calls to the server. They receive back HTML responses which the client then inserts into specific locations in the web page. These applications may sometimes use widget libraries to enhance user experience.
  2. RIA-AJAX. The asynchronous nature still exists. Always uses rich widget libraries (such as dojo, YUI, backbase, tibco GI, etc). But here the event handling and data manipulation is at the widget level (a widget being anything from a text box to a panel or window).
In the RIA-AJAX applications the browser page never refreshes. The client side RIA-AJAX framework provides support in building applications similar to a traditional Windows desktop application. In this scenario the communication between client and server is not HTML. It is some form that allows representing data. XML is one option and JSON is another. JSON support is built into most browsers. Here is a JSON sample.

{
    "phone":{
        "areaCode":703,
        "number":777000
     },

    "age":5,
     "name":"mathew"
}

The XML would be
<root>
   <phone>
       <areaCode>703</areaCode>
       <number>777000</number>
   </phone>
   <age>5</age>
   <name>mathew</name>
</root>

Both formats represent the same data. XML is a little more verbose. For larger files XML is fatty.

It is entirely possible for a web application to send out JSON formatted data to an AJAX client. It is possible to use JSON in a non-UI application too. In either case we need some way to serialize and deserialize JSON to and from Java. Serializing from Java objects to JSON is relatively simpler (note I say relatively). The real challenge is in deserializing incoming JSON to Java objects. I have written some basic Java-to-JSON serializer (supports basic types, wrapper types, complex java objects and arrays of simple type). But have not tried the other way yet.

Also I wonder if we can use XSD to represent the grammer and rules inside a JSON data file. I see no reason why not. Has anyone tried this. Appreciate any pointers. For example given a JSON data file and XSD is there something that can validate the JSON data file?

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments

  • 12/8/2008 9:56 AM Ramagopal Karri wrote:
    Not a direct way, at least that I came across.
    We do this in an indirect way though. We validate the Java object, most usually the POJO, that is used for generating the JSON or is built off JSON. We pass the POJO thru' JAXB (or similar utility) to do the validation. Though it is not the best way to validate a JSON I feel it is useful and may be the only way to use XSD so far till we have a SAX/DOM kind of parser for JSON.
    Problem using JSON with XSD are a bunch like data types. So the JSON parser should be intelligent enough to understand the type, in this case, and convert before validation.
    Reply to this
  • 9/30/2009 7:21 AM geeks wrote:
    Hey, that was interesting,

    Great info on XML.

    Thanks for writing about it
    Reply to this
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.