JSON in Javascript | What is a JSON file ?

What is a JSON file or What is JSON in Javascript?

-> JSON Stands For JavaScript Object Notation and is used to store and transmit data objects consisting of attribute-value pairs (Similar to javascript Objects) and arrays which are human-readable text.’

JSON is a lightweight data storing & transporting format and it is quite easy to understand as it is self-describing. JSON  is called self-describing because one Developer or User can easily understand what data resides inside any JSON document or File. JSON file generally use the extension .json 

Ex- Data.json

The JavaScript library, jQuery also has some useful JSON methods such as getJSON() and parseJSON().

According to some programmers, JSON Is better than The previously used XML. With JSON one can also create complex data structures using nest arrays and objects. JSON is also Beginner Friendly. 

Douglas Crockford was probably the first person to specify and popularize the JSON format. The JSON name came into existence at the State Software which was a company co-founded by Douglas Crockford. 

What is JSON in Javascript?

-> As the name suggests JSON is the JavaScript Object Notation Which was derived from javascript at least for the initial phase. But by late 2016 or the beginning of 2017 many programming languages other than JS started to include code to invoke and parse JSON-format data. 

And JSON became one of the most used and easiest data storing formats. It became a global data format replacing XML. XML is slower and slightly more complex than JSON.

Also Read : 6 Best Android Emulators for PC And Mac In 2021

XML vs JSON

–XML Data Format Example–

<tech_news>

<news>

<name>What is a JSON file</name>

<date>2021</date>

<description>Something</description>

</news>

<news>

<name>What is JSON in Javascript</name>

<date>2020</date>

<description>Something</description>

</news>

<news>

<name>JSON Object </name>

<date>2020</date>

<description>Something</description>

</news>

<news>

<name>What Is JSON in JAVA?</name>

<date>2019</date>

<description>Something</description>

</news>

</tech_news>

— JSON Data Format Example — 

{ “name”:”John Doe”,

   “pet”:”Dog”,

   “male”:true,

   “female”:false,

    “age” : “20”,

     “Married”:false

 }

— JSON Object —

Well, JSON Object can be tricky as it is very similar to javascript object.

But the main difference is the extra quotation of Key, Value pairs. Unlike JavaScript Object, a JSON key has to be a String. The objects in JSON are also surrounded by curly braces {}. Each key and value are separated by a colon (:). Each key and value pair is also separated by a comma.

Javascript Object Example:

let Animals = {

   Animal1 : “Dog”,

   Animal2: “Cat”,

   Animal3: “Cow”,

   Animal4: “Ox”,

   Animal5: “Lion”,

  }; 

To Access a javascript object

 Animals.Animal2 = Cat (Output)

JSON Example:

Genres = { “Name”:”Action”, “filmsNo”:27, “Films”: {

  “film1″:”MIB International”,

  “film2″:”Skyfall”,

  “film3″:”Mortal Kombat”,

   “film4″:”Mission: Impossible Fallout”,

   “film5″:”John Wick”,

  }}; 

Also Read: JSON Objects 

JSON Objects or Nested Objects can be accessed by using either the dot notation or bracket notation.

Also Read : Best WordPress Migration Plugin Free

How to access a JSON Object?

Simple JSON Object ->

 Genres.Name or Genres[“Name”] = Action (Output)

Nested JSON Object -> 

Genres.Films.film5 or Genres.Films[“film5”] = John Wick (Output)

 What is JSON in Javascript & JSON Object

How to Convert a JSON Text or JSON Object to a JavaScript Object?

let anyJSONObject = ‘{ “anime” : [‘ +

‘{ “name1″:”Naruto ” , “type”:”Japanese” },’ +

‘{ “name2”: “DragonBallSuper”, “type”:”Japanese” },’ +

‘{ “name3″:”The Shield Hero” , “type”:”Japanese” } ]}’;

To Convert a JSON Text or JSON Object to a JavaScript Object we can use the built-in function JSON.parse() 

Example : let simpleObj = JSON.parse(anyJSONObject);

How to Convert a JavaScript Object to a JSON Text or JSON Object?

To Convert a JavaScript Object to a JSON Text or JSON Object we can use another built-in function JSON.stringify().  the data has to be a string while sending data to a web server. 

var JSONobj = JSON.stringify(anyJSONObject);

Also Read : 12 Best On Hover Effects In CSS With Example

What Is JSON in JAVA?

Well the JSON in JAVA  is not that different as it is the same JSON format. But to read and write JSON data in Java we have to use The json.simple library. We can also decode and encode JSON objects in java using this library.

Nowadays JSON data format is widely used in APIs. 

Also Read – What Is JSON File?

How to use Fetch API with JSON Data Format?

fetch (JSON file or API Url with JSON Data Format)

.then((data) => {

    return data.json()

  })

  .then((data) => {

    console.log(data)

  })

Image Credits : Unsplash

Learn About JSON vs XML

Leave a Comment