JavaScript JSON
Last Updated :
31 Jul, 2024
JSON, short for JavaScript Object Notation, is a way to organize data. It’s similar to XML in that it structures information, but it’s more lightweight and easier for humans to read and write. Web applications commonly use JSON to exchange data between each other.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is built on two structures:
- A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
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.
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 computer’s entire memory along with the address of the locations that are required, as you might have understood now this is not at all a good way to do it, it is risky to send your entire computer memory.
Here comes JSON to the rescue, JSON serializes the data and converts it into a human-readable and understandable format, which also makes it transferal and to be able to communicate.
Characteristics of JSON
- Human-readable and writable: JSON is easy to read and write.
- Lightweight text-based data interchange format: JSON is simpler to read and write when compared to XML.
- Widely used: JSON is a common format for data storage and communication on the web.
- Language-independent: Although derived from JavaScript, JSON can be used with many programming languages.
JSON Syntax Rules
JSON syntax is derived from JavaScript object notation syntax:
- Data is in name/value pairs Example:
{ "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 commas Example:
{ "name":"Thanos", "Occupation":"Destroying half of humanity" }- Curly braces hold objects Example:
let person={ "name":"Thanos", "Occupation":"Destroying half of humanity" }- Square brackets hold arrays Example:
let person={ "name":"Thanos", "Occupation":"Destroying half of humanity", "powers": ["Can destroy anything with snap of his fingers", "Damage resistance", "Superhuman reflexes"] }JSON Objects
A JSON object is a collection of key/value pairs. The keys are strings, and the values can be strings, numbers, objects, arrays, true, false, or null.
JSON Arrays
A JSON array is an ordered collection of values. The values can be strings, numbers, objects, arrays, true, false, or null.
Example: This example shows the JSON text.
javascript
{
"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"
]
}
]
}
Convert a JSON Text to a JavaScript Object
We will see how to convert a JSON text into a JavaScript Object.
Example: We will be using the JSON.parse() method to convert the JSON text to a JavaScript Object.
JavaScript
let text = '{"model":[' +
'{"carName":"Baleno","brandName":"Maruti" },' +
'{"carName":"Aura","brandName":"Hyndai" },' +
'{"carName":"Nexon","brandName":"Tata" }]}';
const cars = JSON.parse(text);
console.log("The car name is: " + cars.model[2].carName +
" of brand: " + cars.model[2].brandName);
OutputThe car name is: Nexon of brand: Tata
JSON to JavaScript Object
To convert JSON text into a JavaScript object, you can use the JSON.parse() method as shown in the example above. This method parses the JSON string and constructs the JavaScript value or object described by the string.
JavaScript JSON – FAQs
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is often used for transmitting data in web applications.
How do you create a JSON object in JavaScript?
A JSON object is created using JavaScript object notation. It is a text format that represents structured data.
How do you convert a JavaScript object to a JSON string?
You use the JSON.stringify() method to convert a JavaScript object to a JSON string.
How do you parse a JSON string to a JavaScript object?
You use the JSON.parse() method to parse a JSON string and convert it to a JavaScript object.