The X from JSON – first part


jlogo.jpgSometimes happens in this way.
After only two years, even that which seems to be a great love begin to shows its cracks.
As that wasn’t anything, the friends don’t stay on your side: “But are you sure about what you’re doing? Don’t you think that you chose that wrong way, this time too?” “That’s wrong, I believe in this relation, nobody will divide us!”
That ‘nobody’ unfortunately is here now, and has a name: JSON, a new format based on javascript that in these days begin to fight his way in the world of the description and the exchange of the data, introducing itself as an alternative to the old, dear XML.
Before to advance to XML a reflection break that will mean the certain end, I’d like to explore better my heart to find – if not an aswer – at least a suggestion about to with safety some important decisions for our future, whose I don’t stop to believe.

What is XML in few lines

XML is a markup meta-language (a language that describes other languages) customizable, that is to say estensibile, based on text that can be used to structure data showing its meaning.
Making an example, to describe a particular data like “movie” XML allowed us to invent a structure like this:

<movie category="surrealism">
   <title>The phantom of liberty</title>
       <director>Luis Buñuel</director>
       <actor>Jean-Claude Brialy</actor> 
       <actor>Adolfo Celi</actor> 

The document contains a root element (called movie) with an attribute (called category) that together four child elements (which one contains other three child elements) create a particular hierarchy.
All the elements and attributes must to answer to some rules – as the right corrispondence of opening and closing tags, the use of a special syntax with empty tags, the value of attributes inserted in quotes etc. – in other words to display a XML document well-formed. The same document must also be valid : to follow some rules related to logical structure (how many and which tags use, in what position etc.) defined in DTD (Document Type Definition) documents
or in more recent XML Schemas.
Attributes are very important to extend the meaning (or better the semantics) of a particular tag. An example of this is XHTML, a XML-like language which elements represent a series of instruction for the page rendering (we can to consider it a well-formed HTML with some restriction in the DTD) so it’s important to use some attributes to show the semantics of the data that the element is going to include.

Obect notation and JSON

As its acronym describes, Javascript Object Notation, JSON give his vital lymph from javascript objects.
The objects are structures composed by a series of name/value pairs. It can be used in two ways: 1) it’s possible to create the object with the new operator and than initialize the related properties which name follows the name of the object after a point, or are placed between square brackets just after the object name.

var obj = new Object(); = "Table";
obj[material] = "Wood";

2)it’s possible to create the object assigning to it, between braces, a series of name/value pairs (that are divided by a colon) interspaced by a comma.

var obj = {
     name: "Table",
     material: "Wood",
     peculiars: {
         width: 58,
         color: "green"

The second syntax, that you can find in other languages too, is called “Object Literal” and is useful to arrange in groups some variables and functions that work together. (An example of this is done
by general function like init, as suggested in Dustin Diaz and Chris Heilmann‘s posts on the topic).
In 2002 Douglas Crockford thinked to use the object literal to create a format for the data-interchange defined in this introduction page.
As you can seen in various schemas, the principal elements of the JSON syntax are:

  • Objects: an unordered set of name/value pairs separated by a comma and inserted between two braces.
  • Array: an ordered collection of values separated by a comma and inserted within between square brackets. Values are separated by a comma.
  • Values: a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
  • String: a collection of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string

To understand better, I’ll try to re-write the XML’s structure used in the previous example in JSON:

{"movie" : {
   "category" : "surrealism",
   "title" : "The phantom of liberty",
   "cast" : {
   "director" : "Luis Buñuel",
   "actor" : ["Jean-Claude Brialy" ,"Adolfo Celi"], 
    "year" : "1974" ,
   "production" : null

If my translation is exact, it could entail that:

  • The root element (in this case film) become an object placed between two braces.
  • Every root’s child and the other children become a name/value pair.
  • The attributes (here there’s only category) become a name/value pair as it are place between an array)

The introduction’s ‘could’ is required, because in the official site I haven’t find any precise indication that control the structure of a document like in a DTD or in a schema.
In theory, it could be possible to create some instruments (is already enough a debugger javascript?) to judge if a JSON document is well formed , but not to judge if it’s valid.

In the next part we’ll see more in the deeper the differences between the two contenders and the integration with the XMLHttpRequest (AJAX o AJAJ, now?) and in the third part the use with feeds ( and APIs (Yahoo).

One comment:

Leave a Reply

Here's the list of topics.

Here's the months' archive.