Introduction to JSON (JavaScript Object Notation)

Rashmi Bhardwaj | Blog,Programming & Software
Advertisements

There are several open standard file formats available which are used for storing and exchanging data between servers and web pages providing a good alternative to XML which bulky and requires formatting.

Today we look at one such open source file format JSON (JavaScript Object Notation) which is used for sharing data. It is derived from JavaScript programming language but also available for use by many languages – Python, Ruby, PHP, and Java. Let’s look at some of its features and applications.

What is JSON  ?

JavaScript Object Notation (JSON) is a standard text-based format to represent structured data based on JavaScript programming language standard ECMA-262 3rd Edition – December 1999. It is used for sending data in web applications from servers. It is a lightweight data interchange format and language independent. JSON supports arrays, objects, strings, number, and values. It is generally used in REST request and response application program interface (API) services as it is uncomplicated and in readable format it enables faster accessibility, memory optimization, short and simple in nature and does not contain complex syntax and tags.

Advertisements

History  of JavaScript Object Notation

JSON initially was developed due to need for stateless, real time server to browser communication protocol without use of browser plugins such as Flash or Java applets which were used in the early 2000s. Douglas Crockford initially popularized the JSON format. The state of software co-funders agreed to build a system which used standard browser capabilities and provided web developers an abstraction layer to create stateful web applications having persistent duplex connection to a web server. The system was sold to Sun Microsystems, Amazon.com and EDS. The Json.org was launched in year 2002. In year December 2005 Yahoo! started offering some of its web services on JSON.

JSON Data Types

JSON supports below basic data types:

DATA TYPES

DESCRIPTION

EXAMPLES  

NumberDouble precision floating point format in JavaScript. Octal and hexadecimal format are not used , no NaN or infinity is used in number

 

  • Integer – digits 1-9,0, +ve or -ve
  • Fractions like .3, .9 etc
  • Exponent like e, e+ , e-, E, E+, E-

 

 

Syntax: var json-object-name = {string: Number_value, ….}
StringSequence of zero or more double quoted Unicode with backslash escaping

 

  • Single character i.e. a string with length 1

 

Syntax: var json-object-name = {string : “string value”,….}
Boolean True or falseVar json-object-name= {string : true/false….}
Array
  • Ordered sequence of values
  • Enclosed in square brackets
  • Values are separated by comma
  • Array index can start with 0 or 1
  • Array should be used when key names are integers in sequence

 

Syntax: [value , ….]
Value Can be a string, number, true or false , null etc

 

Syntax : string | number | object| Array | True| False|Null
Object
  • Unordered collection of keys : value pairs
  • Keys must be stings and unique from each other
  • Objects to be used when key names are arbitrary strings
Syntax: {string : value, ….}
WhitespaceUsed between pair of tokens and added to make code more readable

 

Syntax: {string: “ “, ….}
NullMeans emptySyntax: var i = null ;

Download the Syntax Table

JSON Syntax rules

JSON syntax is subset of JavaScript syntax and it has some standard syntax rules:

  • Data is in name / value pairs
  • Comma is used as separator for data
  • Objects are enclosed in curly braces
  • Arrays are enclosed in square brackets

JSON data – Name and value – A name / value pair comprises of a field name (in double quotes) followed by a colon and a value

“name” : “John”

In JSON values can be of following data types: a string, a number, an object, an array, a Boolean, null and Json values can’t be a function, a date and undefined

Strings in JSON written in double quotes, numbers must be integers, values in JSON can be object, value in JSON can be arrays, values in JSON can be True/False, values in JSON can be null.

A common use of JSON is exchanging data between web servers. When data is received from web server it is always in the form a string.

Characteristics of JSON

  • It is easy to use and simple
  • It is self-describing
  • It is extensible and Interoperable
  • And language independent
  • JSON is data oriented
  • JSON supports only text and number data type

 

Pros and Cons of JSON

PROS

  • Smaller and easier to convert into data structure
  • Uses minimal formatting
  • Very easy read and very easy to parse

CONS

  • It is not completely secure
  • Supports limited data types
  • No namespace support so poor extensibility
  • No support for formal grammar definition

Applications of JSON

  • Help to transfer data from server
  • JSON file format help in transmitting and serialization of all types of structured data
  • Perform asynchronous data calls without page refresh
  • Transmit data between server and web applications
  • Widely used for JavaScript based application inclusive browser extension and websites
  • JSON can be used with modern programming languages
  • Used for writing JavaScript based applications which include browser add-ons
  • JSON format used by web services and Restful APIs to get public data

Continue Reading:

Introduction to MATLAB

REST vs SOAP: Difference between Web API Services

ABOUT THE AUTHOR


Leave a Comment

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

Shopping Cart