JSON Introduction

JSON Introduction

JSON stands for javascript object notation. It is used to store and transmit data between server and client.

Please keep in mind that JSON data are in key/value pairs that are separated by a comma.

JSON is derived from javascript, therefore, JSON syntax and javascript object literal syntax look like same but both have a little bit different.

JSON Data

JSON data consists of key/value pairs.Key and value are separated by a comma. Key is always exists left side while value lies on the right side.

Keys are always kept within double quotes while value can be put either in the double quote(for string) or without double quote(in case of number).

General Syntax


Example


In this example, "city" is the key while the value is "Salempur". value is put inside double quote due to string.

JSON Object

The JSON object is written inside curly bracket {}. It contains multi-key/value pairs.Each key-value pair is separated by a comma.

Let us understand it with the help of an example

JSON object


JSON Array

It is of two types.

A JSON array is written inside a square bracket []. Let us understand it step by step with the help of an example.

JSON Array of Strings

It only contains string values. Let us see its example.


JSON Array of Numbers

Let us see an example of array of string


JSON Array of Booleans

Let us see it with the help of an example.


JSON Array of Objects

Every object is separated by comma.Let us see its example.


How To Access JSON Data

JSON data can be accesses either by dot operator or square bracket syntax [].

Let us see its example.

Features Of JSON

  • JSON is a light-weight incomparison to XML.
  • Easy to read by humans and machines.
  • JSON is language independent.
  • JSON is used to send data between server and client.

Source Code

Try it now

Example

Source Code

Try it now

Converting JSON To Javascript Object

You can convert JSON data to a JavaScript object using the built-in JSON.parse() function.




General Syntax

Try it now

Source Code

Try it now

Converting JavaScript Object to JSON

You can also convert JavaScript objects to JSON format using the JavaScript built-in JSON.stringify() function. For example.



General Syntax

Try it now

Source Code

Try it now