What is JSON and Where/How to use it?

JSON: JavaScript object notation

What is JSON?

It is structured data which is in a minimal and readable format.

Its main use is to send the data between server and application similar to what we use XML now for.It can be used as alternate to XML and it is a lightweight Data Structure format than XML.Its main benefit is humans can easily read and write data structure in JS0N format. And even machines can easily pass and generate the data in this format.

JSON is a data structure but not a programming language. It has some relationship with JavaScript because it shares some of its syntax rules with what we use in JavaScript. JSON mostly used in ajax application programming. JSON is getting widely used in ReST API implementations to send & received request/response.

Format of the JSON: It will be in structured format with key value pair. It’s format will be very minimal and easily readable. And it has least syntax rules that the value between opening/closing curly brace {} is an object and value between [] is an array.

Sample JSON format:

{name: “XXX”, age: 35, city: “XXXXX”, “Country”: “XXXX” }

JSON Data types:

Number: A signed decimal number

String: zero or more unicode characters which delimanated with double quotes

Boolean: true or false value

Array: arrays starts with square brackets which are an ordered list of zero or more values of any type.

Object: it’s an ordered list of key-value pairs. Objects are deliminator by curly brackets where each key-value pair separated by comma.

Null: an empty value with null word

About Date Format: in JSON response, the date would be in format as “yyyy-MM-dd’T’HH:mm:ss.SSS’Z’.

This date format needs to be converted as per your application needs. You may want to display only in date format or in date+time but no milli seconds or your need may be to store the date in different format.

Here “2010-12-24T10:25:59.191Z if you see there are T & Z in the date string in JSON response. Do you have any idea what are those T & Z represents?

T: is a literal to separate the date and time component

Z: is a zero hour offset which is nothing but UTC. If response is from different time zones, then date time component value automatically gets converted based on the response receiver’s time zone.

Difference Between XML & JSON:

Both XML and JSON are data interchange format where data will be in structured format.

As it has very minimal syntax rules, JSON is not much complex than XML. JSON is less verbose, XML uses many words than JSON do. And JSON parsing is comparatively very faster than XML.

Sample JSON file:

{
"name": "XXX",
"age": 35,
"city": "XXXXX",
"Country": "XXXX"
}

Sample XML file which carries same information as JASON:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <Country>XXXX</Country>
   <age>35</age>
   <city>XXXXX</city>
   <name>XXX</name>
</root>


Example JSON with object and array:

{
"shoppingList": {
"date": "25-12-2000",
"city": "XXX",
"listOfItems": [
{
"productid": "S000123",
"quantity": 1,
"price": 2.6
},
{
"productid": "S000124",
"quantity": 1,
"price": 3.5
},
{
"productid": "S000125",
"quantity": 3,
"price": 9
}
]
}
}

ReST API (REpresentational State Transfer) : many people on internet searching for this word ReST these days to know what is ReST API mean, how we use & where we use ReST. ReST API is an application Programming Interface which uses existing HTTP protocol to interact with server from client.

Mainly there are 6 principles to describe REST API :

it is

  1.     client server style
  2.     stateless
  3.     cache-able
  4.     uniform interface
  5.     layered system
  6.     code on demand (optional)
What HTTP Operations can be used with ReST API?

GET- to list all records with specific input

POST-to create/update a record on server

PUT- to create/update a record

DELETE-to delete a record

PATCH- making partial changes to an existing resource on server

Common HTTP Error Codes:

1xx Informational messages

    •     100 Continue
    •     101 Switching Protocols
  •     102 Processing

2XX Success Response, means the action requested by

the client was received, understood and accepted.

    •     200 OK
    •     201 Created
    •     202 Accepted
    •     203 Non-Authoritative Information
  •     204 No Content

3XX Redirection, means the client must take

additional action to complete the request

    Common codes: 300 Multiple Choices

    •     301 Moved Permanently
    •     302 Found
    •     303 See Other
    •     304 Not Modified
  •     305 Use Proxy

4XX Client side errors, these may be due to some syntax issues in request

    Common codes are 400 Bad Request

    •     401 Unauthorized
    •     403 Forbidden
    •     404 Not Found
    •     405 Method Not Allowed
  •     409 Conflict

5XX Server side errors i.e. request from client received by webserver

but could not responde back with the response

    Comon codes are 500 Internal Server Error

    •     501 Not Implemented
    •     502 Bad Gateway
  •     503 Service Unavailable
4 ways to fix internet explorer browser crash issues
How To Deactivate Your Twitter Account And See Analytics

Leave a Reply