JavaScript JSON

Global Objects - JSON

JSON :

JSON an acronym for JavaScript Object Notation.

It is a lightweight data interchange format used for storing and sharing data between client and server over the network.

For example, to store and share customer information over web, this is how the corresponding JSON data will look like :

Global Objects JSON example

In this code, variable dataJSON is exactly like the literal notation syntax used for object creation in JavaScript. Whereas there is a very small difference.

For JavaScript objects we do not put the key in quotes and if values are of string data type they can be put in single or double-quotes.

But for JSON object, it is mandatory to put the key inside the double quotes and all the values of type string inside the double-quotes.

javascript object vs JSON Object

JSON is a text-only format. It travels over the network as a string.

In this context, there are two important methods given by JSON object. Let us learn and implement them.

JSON object method description and example

Demo :

JS code :

CODE/PROGRAM/EXAMPLE
function User(name, email, mobile, password) {
  this.name = name;
  this.email = email;
  this.mobile = mobile;
  this.password = password;
}
var user = JSON.parse(
  ‘{"name":"Mary","email":"mary123@gmail.com","mobile":1234567890,"password":"mary@123"}’
);
document.write(
  "User Details: " +
    " name is " +
    user.name +
    " mobile is " +
    user.mobile +
    " email is " +
    user.email
);
CODE/PROGRAM/EXAMPLE
var dataJSON = {
	  customers: [
		{ firstName: "Bob", lastName: "Morry" },
		{ firstName: "Albert", lastName: "Smith" },
		{ firstName: "Kate", lastName: "Ward" }
	  ]
	};    
	document.write(
	  "JSON string: " + JSON.stringify(dataJSON));    
	var stringJSON = ‘{"firstName":"Bob", "lastName":"Morry"}’;
	JSON.parse(stringJSON);    
	document.write(
	  "JSON string: " +
		JSON.stringify(JSON.parse(stringJSON) );
#javascript_object_to_json #JSON_object_in_javascript #javascript_json #javascript_json_stringify #javascript_array_to_json #javascript_create_json_object

(New page will open, for Comment)

Not yet commented...