JSON’s full form is JavaScript Object Notation. It is used to transport data in text format. A simple example of JSON is:-
'{"name":"John", "age":70, "city":NY}'
This is an object with 3 properties:- name, age, and city. Where Each property has a value.
JavaScript code access the data as an object:-
let Name = obj.name;
let Age = obj.age;
An important method of JSON in JavaScript
JSON.parse(): for converting JSON strings into JavaScript objects:
JSON.stringify(): for converting an object into a JSON string:
JavaScript object notation example
Simple example code in HTML.
<!DOCTYPE html>
<html>
<body>
<script>
var obj = '{"name":"John", "age":70, "city":"NY"}';
var json = JSON.parse(obj);
console.log(obj);
console.log(json);
</script>
</body>
</html>
Output:
Why Use JSON?
Answer: It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
Read more: https://developer.mozilla.org/
Do comment if you have any doubts or suggestions on this JS object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version