Android UtilityAPI

JSON Data Format : Explanation

What is JSON?

JavaScript Object Notation, It is the data-interchange language independent format and it is easier to understand for both machines and humans. So it is widely used in the area of communication between different software systems.

  • JSON can represent information about a single item or a collection of items.
  • With less content JSON is able to represent complex data in meaningful form.
  • JSON is formed by structuring strings with special characters like, { }, [ ], ‘,’, ‘:’.
  • The basic building block of JSON data is key-value pairs.
  • Key-value pair is expanded into different forms to represent Value, Object and Collections.

Have a look at the below simple JSON string that represents information of this website in JSON.

"info" : { 
           "type"    : "website",
           "url"     : "devdeeds.com",
           "category": "blog"
         }       

Where info is a JSON object and it contains 3 name-value pairs called typeurlcategory. The meaning is very comprehensive and this JSON data can be extended by adding more name-value pairs.

You have noticed one thing that JSON is based on name-value pair architecture. Each element in this format has a value associated with it.

Below is a sample JSON string with different type of values.

{
  "profile": {
    "firstName": "Smith",
    "lastName": "John",
    "age": 30,
    "gender": "Male",
    "address": {
      "street": "20th 2nd Street",
      "city": "New York",
      "state": "NY",
      "postal_code": "10003"
    },
    "contact": [
      {
        "type": "Home",
        "number": "(735) 754-0100"
      },
      {
        "type": "Office",
        "number": "(725) 854-0750"
      }
    ],
    "marital_status": true
  }
}

Why JSON?

I remember the time when developers use delimiter string to represent data and working with delimiters was a nightmare for all developers until Douglas Crockford specified a new data format called JSON, JavaScript Object Notation.

As i said earlier, the primary usage of this data format is in the area of data  communication. Every software system needs to communicate with other for data exchange and it should be able to represent the state-full data and communication should be as faster as possible.

When we transfer a data from one place to other,  few things we have to concerned about that are as follows,

1. Sender

Sender, it is the source of data or creator. Sender should be able to present complex data and it should be able to understandable by the receiver.

2. Medium

Medium is internet here. Which is concerned about the amount of the data to be transferred. The performance of the medium is assessed by the amount of data is transferred.

3. Receiver

Receiver should able to retrieve values from the data format without any complication and delay.

If above mentioned three points can be satisfied by a data format then without any doubt we can call it good data format.
JSON is not only good but also best in all aspects. Because of these points,

  1. Light-Weight
  2. Program Language Independent
  3. Easy to understand by humans
  4. Capable of representing complex information in minimum data
  5. Optimized

If you know JSON, whenever you are requested to do a data exchange then the first format will come to your mind is JSON. Because it is the most efficient way to transfer/ exchange data between two end points. This is the reason why it has become an inevitable part of software development nowadays.

XML and JSON are popular for representing complex data structures, in this JSON can do it in less data and lighter than XML. When OOps has opted by many software programming languages and one of the biggest challenge they faced was to present/exchange data in textual form.

Basic Structure

When it comes to format, the structure is everything. Even silly mistakes in structure makes entire data wrong.

Wrapper

The entire data should be wrapped either with an object or Array. Otherwise root level will have multiple parents it is a wrong format.

Name-Value pairs

It is the building blocks in the JSON format. Each name value pairs every name has a value associated with it. Value can either be a string, object or array.
With string we can define data type like, integer, string, float, boolean. With object we can define objects as value. With array we can define ordered list.
Each name-value pairs are separated by commas.

Syntax

{} represents an object in the data. It should have an opening and closing brackets.
[] represent an Array in the data. It should have an opening and closing brackets.
, is used as the separator if there is more than one value present in the same level.
: used in name value pairs as assignment operator, “equal to” (=)

{} represents an Object

"person" : {"name" : "Smith"}

Here person is the JSON object with single attribute called name.

[] represents an Array

Array is primarily used in ordered listing purpose. If there are more than one value is present against a key then Array is used to list those values. In the above sample , contact is an example for JSON array. It keeps the order. It is more important when you are working with price list etc where order is very important.

“contact”   :[{
			“type” : “home”,
			“number” : “(735) 754-0010”
               },
	       {
			“type” : “office”,
			“number” : “+1-777-531-0010”
	       }]   

‘,’ is the Separator

We have seen that JSON is a collection of name-value pairs. So it will have multiple set of name-value pairs. Comma (,) separates these pairs.

           "type":"website", "url":"devdeeds.com"  

Hierarchical Representation Of JSON Data

In this view each name-value pairs are drawn as nodes. Data is drawn from top to bottom fashion.

json_hierarchy

JSON in APIs

It is important that the data that is going to interchange between two different platforms should be in understandable format by both ends.
When using JSON, it has more advantages than any other data formats. Because APIs are accessed through internet and therefore speed of data transfer matters alot. So lesser the data faster the transmission speed. Parsing time is also depend on size of the data. JSON has all these advantages. For these reasons JSON has been opted by most of the API developers as the preferred data format.

Parsing JSON data

Parsing is the process of reading and retrieving values of each node. We can also say that the parsing is the process of converting each node into corresponding program variable. For ex, if JSON object person is converted into program object then it becomes persion object and it has all the properties defined under that node like, name,age, gender…

About author

Geethu is a teacher by profession, but she likes to research new things and is also interested in writing. Devdeeds is her blog, where she writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

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