JavaScript JSON

JSON: JavaScript Object Notation

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

JSON data format
JSON keep 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": "p@test.com", "id": "345"}';
var jsonData = JSON.parse(jsonString);
document.write(jsonData.email);
 
var jsonString = {name:"Rupali", email: "r@test.com", id: 123};
var jsonData = JSON.stringify(jsonString);
document.write("</br>");
document.write(jsonData);
</script>
 
</body>
</html>

Output

p@test.com
{"name":"Rupali","email":"r@test.com","id":123}
Please Share