Skip to content
Related Articles

Related Articles

JavaScript JSON

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 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 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:

    var person={ “name”:”Thanos”, “Occupation”:”Destroying half of humanity” }

  • Here person is the object.

  • Square brackets hold arrays
    Example:

    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" ]
        }
    ]
}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!