JSON stands for JavaScript Object Notation
JSON is a lightweight format for storing and transporting data
JSON is often used when data is sent from a server to a web page
JSON is "self-describing" and easy to understand
This example is a JSON string
{"name":"john", "age":30,"car":null}
It defines an Object with 3 properties
- Name
- Age
- Car
Key and Value
The two primary parts that make up a JSON are key and values:-
Key: A key is always a string enclosed in quotation marks.
Value: A value can be a string number, boolean expression, array or object.
{"name":"john"}
The key is "name" and the value is "John"
JSON Methods
JSON.parse( )
A common use of JSON is to exchange data to/from a web server. When recieving data from a web server, the data is always a string
"{"name":"john", "age":30,"car":null}"
We use the JSON.parse() to convert strings into Javascript Objects
const obj=JSON.parse("{"name":"john", "age":30,"car":null}");
JSON.stringify( )
When sending data to a web server, the data has to be a string.
Convert a Javascript object into a string with JSON.stringify( ).
Imagine we have this object in Javascript
const obj={"name":"john", "age":30,"car":null};
const myJSON=JSON.stringify(obj);
Credits: coding_.master on IG:- For his inspirational post on Javascript and a schematics on how to write this article and also w3Schools