What is JSON?
When we working with APIs we use JSON objects as responses But what is a JSON object ? This page will give you a brief idea about JSON objects and the use of JSON.
INTRODUCTION
The JSON or ‘JavaScript Object Notation’ is a format of data sharing and derived from JavaScript. But it’s available for other languages like Python, PHP, Java as well. Uses the .json extension when stand-alone and when using inside another file, can be an object assigned to a variable or it can be a String which written inside quotes.
JSON is very lightweight, a good alternative to XML, and less formatting is required.
STRUCTURE OF JSON OBJECT
JSON data are similar to JavaScript properties and contain key-value pairs. Each key-value pair of a JSON object is separated by a comma(,). JSON key (“key”) is written inside quotes and the value assigned to the key is separated by a colon(:).The value of a key can be a string, number, boolean, Array, or null. A key can be any string but if it has more than one word it’s better to use an underscore(“my_name”) than using white space(“my name”) to separate the words since adding white space between the words can lead to issues when accessing JSON data. All the keys inside a JSON object should be unique within that object.
//JSON OBJECT
const User = { "name" : "John",
"age" : 25 }
//name is thkey and John is the value
ACCESSING JSON DATA
To access JSON data need to use the dot notation(variableName.key). Using square brackets also JSON data can be accessed(Variablename[“name”]).
const User = { "name": "John",
"age": 25 }
// accessing JSON object using dot notation
console.log(User.name); // John
//accessing JSON object using []
console.log(User["name"]);//John
JSON VS JAVASCRIPT OBJECT
Though JSON similar to JavaScript objects JSON is different from JavaScript objects.
Keys are not quoted in JavaScript objects, and JSON keys are quoted.
JSON cannot contain functions but JavaScript objects can.
//JSON OBJECT
const User = { "name": "John",
"age": 25 }//JavaScript object
const person = {
firstName: "John",
lastName: "Doe",
fullName: function () { //function
return this.firstName + " " + this.lastName;}
};
CONCLUSION
JSON is language-independent and lightweight. Most commonly used for data interchange.JSON has been experiencing increased support for APIs as a format.
And this the end of the brief note of JSON.
Did you know these details about JSON?
Thank you for reading!!