What is JSON?

This tutorial explains the use of JSON and how to use it in your programming. This tutorial/webpage regularly updated with latest information about the JSON and Java libraries that are used for parsing the JSON files.

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”.

JSON Structure

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.

JSON Tutorials

  • Creating JSON using Groovy : This article explains a simple example of creating JSON using Groovy. This article would be using JSON as the response format in the REST API.
  • Creating JSON Data Using The Java JSON API (JSR 353) : JSON has been around for quite sometime now and lot of languages are offering out of the box support for parsing and creating JSON data. The JDK didn’t have support for parsing and creating JSON data out of the box. One had to make use of libraries for parsing and creating JSON data like Google GSON among others. With JSR 353 and its reference implementation we can hope that it becomes a part of JDK so that we are less dependent on 3rd party libraries.
  • Using JsonSlurper Groovy API from Java to parse JSON : In this example we use JsonSlurper in Groovy to parse the JSON documents from Java. Sounds cool right! In one of the talks on Functional Programming, Venkat showed us how we could exploit the Software Transactional Memory feature in Clojure to create concurrent applications in Java with less pain.
  • Creating JSON document using Java and GSON API : We saw how to parse JSON using GSON API, in this post we would take a look at how the same GSON API can be used to create JSON from the Java objects. Please read this to get an idea of what GSON is. Diving right into the topic of this example, we would make use of the toJson method of com.google.gson.Gson class.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!