Tuesday, April 16, 2013

Understanding the concept of JSON

When you were a child you used to request chocolates from your dad and he used to buy it for you, really childhood memories are awesome.

In todays session we are going to look closely at JSON, which happens to be the preferred option that web developers are using as an output response.

Well consider your childhood memories for a second, your iPhone is the child who will request chocolates (i.e. data for a particular request) from the server who’s the dad here and the server (dad) will do the required processing for the requested chocolate (data) and will give the chocolate to the child (iPhone), this is what happens in all the iOS application that have server request involved.

The processing on the iPhone should be fast, your user should not wait long to see his requested data, so it all depends upon how quickly you can parse (read) the response given by the server and display it in the iPhone UI (user interface).

Keeping in mind that we are working with an environment where speed is the key, JSON provides a better solution in parsing the response returned by the server.

JSON (JavaScript Object Notation) it’s a response format, which is composed of key value pairs, you supply a key and get the value from it just like you do with NSDictionary class.

A JSON response may contain a single dictionary with array or a single array with multiple dictionaries or a single dictionary with some keys or all of them, the structure of the response depends upon what you have requested from the server, so you can say that JSON is a container of your requested data from the server but in key value format.

You request your query to the server with the help of NSURLConnection class which later provides you with NSData object from the server in it’s delegate method named “connection did finish”, this NSData object is your actual response which you have received for the request you made, but you cannot read this response since it’s in bytes.

In order to work with JSON we have a class called as NSJSONSerialization which helps us to convert the NSData object into actual readable JSON key-value pair type, earlier we used a third party library called as the SBJSON.

You might have a question here like how can we identify if the JSON format is a array or dictionary, well the answer is very simple if the JSON format contains a “{} “ curly bracket then its clear that it’s a dictionary and if the JSON response has a square bracket “[]” then it’s an array.

Few years ago i worked on a project which had a really nice and a complicated JSON structure which i would like to share with you, hope this will help you in the learning curve.

Image 1 : JSON is a container of your data which you get from the server.

Look at the above image the response format is in curly bracket and hence we can say that it's a dictionary type, and if the response was an array then it contained square bracket [].

Image 2: After expanding the JSON tag i can see that my response contains two set of arrays and one key named status.

Image 3: After expanding the result tag which is an array you can see that it contains a list of twenty dictionaries within itself at various index.

 Let's expand a dictionary from the result tag and see what it contains

Image 4 : The dictionary present at the 0th index of the result tag array again contains multiple objects of various types

By looking at the above structure we can conclude that a JSON is a container of our data which we request from the server and each item present in the container can have multiple items of their own in key-value pair.

I use a link to identify and study the JSON response form the server so that I can code accordingly hope this link will help you to Online JSON Reader.

I hope that after reading this post your concepts of JSON will be more clear and you can now identify the JSON structure and code as per your server response.

You may enter your queries or suggestions via mail or comments and i will revert back to you, until then Happy iCoding and have a great day.

Join Us on Facebook


  1. Very Very useful post for learning JSON...
    Thank you so much for writing these one for us.....
    Very clearly explained json and in very easy way...today got each and evry point clear for JSON...now waiting for the code part...

  2. nice explanation Sir.....
    Thanks for it..

  3. thank you so much ravi...i am waiting for next part.

  4. iPhone herstellingen bij iPhonedokter. Wij zijn een premium iphone reparatie center voor allerlei smartphones zoals iPhone, iPod, iPad, apple, blackberry, Samsung, Nokia, etc. Alle phones herstellen door ons met garantiThe uthIfie.

  5. Definitely, this blog is full of good posts. I am loving, going through all of them.I really do appreciate this great information.

    Apple service center in Delhi

  6. please provide how to read web services data
    through JSON (explen programatically).

  7. how to add json framework in our progam

  8. @unknown : You may use a third party library called as SBJSON or if you are building an app is from iOS 5.1 onwards then there is a class called as NSJSONSerialization which you can use read about it at the below link :