What is JavaScript Object Notation (JSON)?

A quick JSON search on Google would yield plenty of results and more specifically the JSON homepage. None the less we wanted to share something brief and something good about JSON. Lot of you are familiar with XML and XML had been the defacto data-transfer language in the area of RESTful webservices, but off late JSON has been gaining popularity for reasons which I would explain a bit further. Before going further into XML versus JSON, let me give a brief overview of the structure of JSON documents. And for those not aware of what JSON stands for, its JavaScript Object Notation. I know the use of “JavaScript” term has more relevance and is not like the use of “Java” in the term “JavaScript”.

A JSON structure consists of name-value pairs where a collection of such name-value pairs is called an Object, and each value could be an object themselves or be some primitives like Strings, Numbers and so on. Also values can be arrays of objects or primitive values. Sounds confusing right, let me explain with an example, consider a JSON document retrieved from http://api.twitter.com/1/trends/available.json which shows the available twitter trends and their location information. Let me get a small excerpt from that document:

[
 {
   "parentid": 23424900,
   "name": "Mexico City",
   "url": "http://where.yahooapis.com/v1/place/116545",
   "country": "Mexico",
   "countryCode": "MX",
   "placeType": {
     "name": "Town",
     "code": 7
   },
   "woeid": 116545
 },
 {
   "name": "Jackson",
   "url": "http://where.yahooapis.com/v1/place/2428184",
   "country": "United States",
   "countryCode": "US",
   "placeType": {
     "name": "Town",
     "code": 7
   },
   "parentid": 23424977,
   "woeid": 2428184
 }
]

In the above example, the notation [ ] is used to represent the Arrays and anything within { } is considered to be an object literal/ or an object. We can observe that we have an array of two objects where each object represents some twitter trend details. Digging further we can observe that objects have a “name”:value pattern, this is somewhat closer to our Map representations. The “placeType” attribute has another object as its value, similarly attributes like “code”, “woeid” have Numbers as their values.

Just in case you want to learn more about this structure/syntax, you can visit JSON.org for a beautiful diagrammatic explanation.

Origin and Use of JSON: 

Douglas Crockford of JavaScript Good Parts fame was the earliest promoters of JSON notation and it was named so because  it mimics the object notation used in JavaScript. It doesn’t require JavaScript to use but its fairly easy to create JavaScript objects using the JSON data. There are lot of APIs in almost all languages which provide support for parsing and creating JSON documents and languages like Groovy provide such support within their standard APIs. NoSQL databases like CouchDB, MongoDB extensively use JSON for representing the data.

Lot of RESTful Webservice APIs out there support both XML and JSON data formats. And with JavaScript frameworks like Backbone.js it becomes really easy to consume the JSON documents provided by these RESTful APIs and create model objects. An advantage of JSON over XML is that, its really less verbose than XML. And also the JSON representation is more closer to the Object/Key-value kind of notation which lot of us Java programmers are familiar with.

In the coming posts, we would be writing on how one can parse JSON in Java and other JVM languages and also how to use it in parsing the data from a RESTful API. So before that, we thought of giving a brief overview of JSON.

Any of you have more to share about JSON, any good/bad experiences of using JSON, do leave them here as comments, we would be more than happy to update the post to add your comments.

Comments

comments

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Trackbacks

  1. [...] brief about MongoDB: It is a document oriented NoSQL database, which stores the data in the form of JSON-like document structure. I can go one and write more, but we can explore as we go [...]

  2. [...] version of jQuery is 1.8. We can use jQuery to manipulate DOM elements, send Ajax request, receive JSON response, perform animations, and develop user interface widgets and much more. jQuery also [...]

Speak Your Mind

*