Introduction to JSON (JavaScript Object Notation)

JSON (JavaScript Object Notation) is an open-standard lightweight data-interchange format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is subset of JavaScript. It uses conventions that are familiar to programmers of the C family of languages like C, C++, C#, Java, JavaScript, Perl, Python.

JSON is a text format. Hence, it  is language independent. Data can be stored and exchanged using this format. It is often used to exchange data between different platforms and is a fast alternative to XML in Ajax requests.

The file type for JSON files is “.json” and the MIME type is “application/json”.

Why use JSON?

JSON is easy for humans to read and write; easy for machines to parse and generate. Furthermore, it offers the following features which makes it outstanding:

Standard Structure: JSON follows a standard structure, which makes developers job easy to read and write code, because they know what to expect from it.

Light weight: JSON is light weighted. Because of this, JSON makes easier to get and load the requested data quickly.

Scalable: Since JSON is language independent, it can work well with most of the modern programming language across various platforms.

Why Use JSON over XML?

» JSON is lighter as it requires less tags than XML.
» It is more compact than XML and hence it  loads quickly.
» It is quicker to read and write than XML.
» JSON Objects are typed (string, number, array, boolean, object) whereas XML data is typeless (only string).
» Data is readily accessible as JSON objects for JavaScript code whereas XML data needed to be parsed and assigned to variables through tedious DOM APIs.

JSON Syntax

It uses JavaScript object notation syntax:
» Data is in name/value pairs separated by : (colon)
» Data (name/value pairs) is separated by , (commas).
» Objects begin with { (left brace) and ends with } (right brace)
» Square brackets ([]) hold arrays.

Example:

// Simple
{ "name":"The Debuggers" }

// With more information
{
"author":{ "name":"The Debuggers", "gender":"Male","post":100 }
}

Some built in JavaScript functions to work with JSON

  • JSON.parse() – Converts a string, written in JSON format, into native JavaScript objects.
  • JSON.stringify() – Converts a JavaScript object into a string.

Leave a Reply