What is JSON?
JSON stands for JavaScript Object Notation
It is an open standard file format and data exchange format that employs text that humans can read to store and send data objects made up of attribute-value pairs and arrays (or other serializable values). It is a widely used data format for electronic data exchange, notably between servers and online applications.
JSON file is saved with .json xxtension.
JSON Types
- Strings :
"Krutik Raut"
"Hello World"
- Numbers:
13
0.6
-21
- Booleans:
true
false
- Arrays :
[1,2,3]
["Hi", "Hashnode"]
- Objects:
{"key":"value", "name": "Krutik"}
Note: JSON names require double quotes. JavaScript names do not.
JSON Syntax
- Data is in name/value pairs
- Commas are used to separate data
- Curly braces hold objects
- Square brackets hold arrays
Example:
{
"name":"Krutik",
"age":20 ,
"isDeveloper": true,
"languages": ["HTML","CSS","JavaScript","PHP"],
"Idk":null,
"pets":[{"name": "Robert","type" : "Dog"},
{"name": "Max", "type" : "Cat" }]
}
How to access data from a JSON file or Object?
If we load this data as a variable called info, we could then access the data inside it using the same dot/bracket notation. For example:
info.name
or info['name']
Output:
"Krutik"
info.pets[1].name
Output:
"Max"
info.languages[2]
Output:
"JavaScript"
Why use JSON?
The syntax of the JSON format is similar to that of the JavaScript object creation code. This makes it simple for a JavaScript code to turn JSON data into JavaScript objects.
JSON data may be used in any programming language and is easily transferable between machines because it is merely in text format.
Things to keep in mind while writing JSON
- JSON consists solely of a string with a predefined data format.It has no methods,only properties.
- JSON requires double quotes to be used around strings and property names. Single quotes are invalid.
- A single missed comma or colon might cause a JSON file to stop working.