JSON stands for “JavaScript Object Notation” which is a lightweight , language independent and self describing format in text for data storing and interchange. JSON is easy to read, write and create for humans which makes it famous over XML.
JSON was derived from JavaScript but now it is supported by many languages. A file containing JSON is saved using “.json” extension.
When we want to create a new resource or say want to add a new student details in to a student database, we need to send a POST request with a payload to API end point. This payload can be a JSON format. So , JSON is majorly used to exchange data between Web Server and Client.
An example JSON is below:-
{
"firstName":"Amod",
"lastName":"Mahajan",
"age": 28,
"salary": 10000.56
}
As you see above, JSON stores data as a key-value pair. Key is left side and value is right side and a semi colon is used to separate both. One key-value pair is separated with another key-value pair using comma (,).
A key is always a string and a string must be enclosed in double quotes. A value can be a string, number ( With decimal and without decimal ), a Boolean ( true and false) , an object , an array or a null.
JSON can be found in two formats :- JSON Object and JSON Array. Example shown above is a JSON Object. Below is a JSON Array:-
[
{
"name": "Amod",
"skills": "Selenium"
},
{
"name": "Mukesh",
"skills": "API"
}
]
A JSON Object is an unordered data structure which starts with opening braces ( ‘{‘) and ends with a closing braces (‘}’). A JSON Array is a ordered collection which starts with opening bracket ( ‘[‘) and ends with a closing bracket(‘]’). A JSON array consists of values separated by comma. A JSON array can be hold multiple JSON Objects as well.
Example:-
{
"firstName": "Amod",
"lastName": "Mahajan",
"age": 28,
"salary": 10000.56,
"Mob": [
1234567890,
1023456789
],
"Address": [
{
"addressType": "Home",
"city": "Bengaluru"
},
{
"addressType": "Office",
"city": "Mumbai"
}
],
"isMarried": false,
"techSkills": {
"programmingLangauge": "Java",
"UIAutomation": "Selenium",
"backendAutomation": "API"
},
"Nationality": "Indian",
"hobbies": null
}
Above JSON contains example for all possible types of values. Key “Mob” is a JSON array of strings while “Address” key is a JSON array of JSON Objects. “isMarried” key holds a boolean value. Key “techSkills” holds a JSON Object. Key “hobbies” holds a null value. “NULL” in place of “null” will give an error.
There are multiple websites which you can use to create and validate JSON easily.