DEV Community

Roshan Shambharkar
Roshan Shambharkar

Posted on

What is JavaScript JSON📖

✔ JSON stands Form JavaScript Object Notation(JSON), JSON is Text Based Data Format, that is used to store and transfer data,
it is usually useful to sent to data server to webpage,

Syntax of JSON

{
 "MoviesStar":[
  {"firstName":"John","lastName": "cena"},
  {"firstName":"linda","lastName": "Friday"},
  {"firstName":"Teresa","lastName": "Plamer"},
]
}
Enter fullscreen mode Exit fullscreen mode

🔴This above JSON Syntax defines movie star name object: an array of 3 moviestar record

⭐In JSON data stored in KEY/VALUE Pair separate by comma,

JSON DATA

JSON data is written as name/value pairs, just like JavaScript object Properties, A name and value pair consists of field name(in double qoutes), followed by a colon, followed by value,
e.g "name": "Tony"

JSON Objects

The JSON object is written inside curly braces{}, JSON objects can contain multiple key/value pairs,

for e.g :-

{
   "firstName":"Tony",
   "lastName": "stark"
}
Enter fullscreen mode Exit fullscreen mode

JOSN Arrays:

JSON array is written inside square Brackets[].
for e.g:-

//JSON array
["audi", "BMW", "jaguar"]


//JSON Array containing  objects
[
  {"name": "Tony", "age": 48},
  {"name": "Peter", "age": 20}
]
Enter fullscreen mode Exit fullscreen mode

🚫Note:
JSON data can contain objects and arrays. However, unlike Javascript objects, JSON data connot contain functions as value.

Accessing JSON data
We can access JSON data using the dot notation LIKE this,

//JSON object
const data = {
"name": "john",
"age":  22,
"hobby": {
"reading": true,
"gamming" false,
"sport": "football"
},
"class":["JavaScript", "HTML", "CSS"]

//accessing  JSON object
console.log(data.name); //john
console.log(data.hobby); //{"reading": true,"gamming" false,"sport": "football"}

console.log(data.hobby.sport); //football
console.log(data.class[1]); //HTML
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)