Sunday, 28 September 2014

Javascript Object Notation (JSON)

JSON is a data format. It is an alternative to other data formats, e.g. XML.
To get a feel of JSON, look at the example below:

{
    "Name" : "Alice",
    "Gender" : "Female",
    "Age" : 11,
    "Languages" : [ "English", "Japanese" ],
    "LastKnownLocation" : {
        "City" : "Kuala Lumpur",
        "State" : "Selangor",
        "Country" : "Malaysia"
    },
    "Alive" : true
}

Here is the same data in XML format:

<Name>Alice</Name>
<Gender>Female</Gender>
<Age>11</Age>
<Languages>
    <Language>English</Language>
    <Language>Japanese</Language>
</Languages>
<LastKnownLocation>
    <City>Kuala Lumpur</City>
    <State>Selangor</State>
    <Country>Malaysia</Country>
</LastKnownLocation>
<Alive>true</Alive>

The kinds of values supported by JSON are strings (Name and Gender), numbers (Age), objects (LastKnownLocation), arrays (Languages), booleans (true/false e.g. Alive), and null.

JSON offers the advantage that they can be used "natively" by languages such as Javascript. No need for an additional parsing library!

No comments:

Post a Comment