JavaScript JSON - GeeksforGeeks

23 Feb, 2022
What is JSON?JSON or JavaScript Object Notation is a format for structuring data.
What is it used for?Like XML, it is one of the way of formatting the data. Such format of data is used by web applications to communicate with each other.
Why JSON?
The fact that whenever we declare a variable and assign a value to it, it’s not the variable that holds the value but rather the variable just holds an address in the memory where the initialized value is stored. Further explaining, take for example:
let age=21;
when we use age, it gets replaced with 21, but that does not mean that age contains 21, rather what it means is that the variable age contains the address of the memory location where 21 is stored.
you might think what is the problem, how is JSON helpful?
well, yes, you are right! it is fine here till now but imagine you have to transfer the data and use it somewhere else (like an API maybe), so how will we share this? One way could be to send your computers entire memory along with the address of the locations that is required, as you might have understood now that this is not at all a good way to do it, also it is risky to send your entire computer memory. Here comes JSON to the rescue, JSON serializes the data and converts it into human-readable and understandable format, which also makes it transferal and to be able to communicate.
Characteristics of JSON
It is Human-readable and writable.
It is light weight text based data interchange format which means, it is simpler to read and write when compared to XML.
It is widely used as data storage and communication format on the web.
Though it is derived from a subset of JavaScript, yet it is Language independent. Thus, the code for generating and parsing JSON data can be written in any other programming language.
JSON Syntax RulesJSON syntax is derived from JavaScript object notation syntax:
Data is in name/value pairsExample:{ “name”:”Thanos” }Types of Values:Array: An associative array of values.Boolean: True or false.Number: An integer.Object: An associative array of key/value pairs.String: Several plain text characters which usually form a word.
{ “name”:”Thanos” }
Types of Values:Array: An associative array of values.Boolean: True or false.Number: An integer.Object: An associative array of key/value pairs.String: Several plain text characters which usually form a word.
Data is separated by commasExample:{ “name”:”Thanos”, “Occupation”:”Destroying half of humanity” }
{ “name”:”Thanos”, “Occupation”:”Destroying half of humanity” }
Curly braces hold objectsExample:var person={ “name”:”Thanos”, “Occupation”:”Destroying half of humanity” }Here person is the object.
var person={ “name”:”Thanos”, “Occupation”:”Destroying half of humanity” }
Here person is the object.
Square brackets hold arraysExample:var person={ “name”:”Thanos”, “Occupation”:”Destroying half of humanity”,“powers”:[“Can destroy anything with snap of his fingers”,“Damage resistance”, “Superhuman reflexes”] }Here person is the object and powers is an array.
var person={ “name”:”Thanos”, “Occupation”:”Destroying half of humanity”,“powers”:[“Can destroy anything with snap of his fingers”,“Damage resistance”, “Superhuman reflexes”] }
Here person is the object and powers is an array.
Examples:
{    "Avengers": [          {          "Name" : "Tony stark",          "also known as" : "Iron man",          "Abilities" : [ "Genius", "Billionaire",                        "Playboy", "Philanthropist" ]        },          {          "Name" : "Peter parker",          "also known as" : "Spider man",          "Abilities" : [ "Spider web", "Spidy sense" ]        }    ]}
perveenneha3
javascript-basics
JSON
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
How to append HTML code to a div using JavaScript ?
File uploading in React.js
Hide or show elements in HTML using display property
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request