JavaScript JSON

JSON: JavaScript Object Notation

The JavaScript JSON is a lightweight human-readable collection of data that is used to get a format for storing and transporting data. It is commonly used for data transfer as an alternative to XML.

JSON data format
JSON keeps data in name-value pairs, separated by commas. Curly braces are used to hold objects and square brackets are used to hold arrays.

Syntax 1: For .json files:

{"First_Name" : "value"; "Last_Name": "value ";}

Syntax 2: For JSON objects:

var varName = { "First_Name" : "value";"Last_Name": "value "; }

JavaScript JSON Methods:

parse() method:
Use: To transform a JSON string into a JavaScript object.

stringify() method:
Use: To transform a JSON object into a JSON string representation.

Example:

<!DOCTYPE html>
<html>
<body>
<script>
var jsonString = '{"name":"Pooja","email": "[email protected]", "id": "345"}';
var jsonData = JSON.parse(jsonString);
document.write(jsonData.email);
var jsonString = {name:"Rupali", email: "[email protected]", id: 123};
var jsonData = JSON.stringify(jsonString);
document.write("</br>");
document.write(jsonData);
</script>
</body>
</html>