JavaScript – JSON


What is a JSON ?

JSON is Javascript Object Notifications.

JSON provides the format for storing and processing data.

JSON format will be in human readable collection of data.

What is a JSON in javascript ?

JSON is the one other way for formatting the data.

This format is widely used in web applications for communication between

1. client to server

2. server to client

3. server to server

Javascript beginner course !!!

Click to Learn More about – Javascript online learning

Syntax :

JSON syntax is derived from Javascript

JSON should always contain “key” and its “value” seperated by semicolen(:).

Many key-value pairs can be included in JSON Object.

	{  
            "key1" :  "value1";  
            "key2" :  "value2";  
            "key3" :  "value3";  
            "key4" :  "value4";  
	} 

What are the methods for JSON in Javascript


Let’s see the list of JavaScript JSON method with their description.

There are two methods for JSON in javascript, listed below

1. JSON.parse()

2. JSON.stringify()

1. JSON.parse() method

JSON.parse() method takes a JSON string and transforms/modifies it into a JavaScript object.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
		var a = '{"Name":"AbdulKalaam","Age": "87","Country": "India"}';  
		var data = JSON.parse(a);  
		document.write("Converting string to JSON using parse() method<br>");  
		document.write("The JSON value of key Name = "+data.Name+"<br/>"); 
		document.write("The JSON value of key Age = "+data.Age+"<br/>"); 
		document.write("The JSON value of key Country = "+data.Country+"<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :

Converting string to JSON using parse() method


The JSON value of key Name = AbdulKalaam

The JSON value of key Age = 87

The JSON value of key Country = India

2. JSON.stringify()

JSON.stringify() method converts a JavaScript value (JSON object) to a JSON string representation.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
		var a = '{"Name":"AbdulKalaam","Age": "87","Country": "India"}';  
		var data = JSON.stringify(a);  
		document.write("Converting string to JSON using stringify() method<br>");  
		document.write("The String data  = "+data+"<br/>"); 
		</script>
	</head>
	<body></body>
</html>

Output :

Converting string to JSON using stringify() method

The String data = “{\”Name\”:\”AbdulKalaam\”,\”Age\”: \”87\”,\”Country\”: \”India\”}”