Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJSON vs JavaScript Object

JSON vs JavaScript Object

In this article, we will learn about JSON and JavaScript Object and the key differences between them.

What is JavaScript Object?

The foundation of contemporary JavaScript is made up of objects, which are the most significant data type in JavaScript. The primitive data types in JavaScript (Number, String, Boolean, null, undefined, and symbol) are significantly different from these objects in that they all only hold a single value (depending on their types).

 

Syntax:

let object_name = {
    key_name : value,
    ...
}

Example:

Javascript




let school = {
    name: 'Vivekananda School',
    location : 'Delhi',
    established : '1971',
    20 : 1000,
    displayInfo : function(){
          console.log(`The value of the key 20 is ${school['20']}`);
     }
}


In JavaScript, Objects can be created using two different methodologies namely Literal Form and Constructed Form.

What is JSON?

JavaScript Object Notation, or JSON. It is a method of organizing data. Different online apps communicate with one another using this format. It is the XML data interchange format’s successor. Compared to XML, the data is simple to structure. It supports the JSON documents that are quickly performed on the server, as well as data structures like arrays and objects.

It can be also accessed using an index just like an array or string. JSON array can be of any data type. Values within a JSON array are separated by commas and are encapsulated in curly brackets. In python, a JSON array is just like a python dictionary in which key-value pairs are stored in an ordered manner. Data is separated by commas.

Syntax: JSON syntax is derived from JavaScript object notation syntax:

Data is in name/value pairs

For 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.

Syntax:

// Basic JSON object
{ 
    “name”: ”Thanos”, 
    “Occupation”: ”Destroying half of humanity” 
}
// Curly braces hold objects

var person = { 
    “name”: ”Thanos”, 
    “Occupation”: ”Destroying half of humanity” 
}
// Here person is the object.

// An array inside object
var person = { 
    “name”: ”Thanos”, 
    “Occupation”: ”Destroying half of humanity”,
    “powers”: [
        “Can destroy anything with snap of his fingers”,
        “Damage resistance”, 
        “Superhuman reflexes”
    ] 
}

Example:

Javascript




{
    "Data Structures": [
        {
            "Name" : "Trees",
            "Course" : "Intoduction of Trees",
            "Content" : [ "Binary Tree", "BST",
                       "Generic Tree"]
        },
        {
            "Name" : "Graphs",
            "Topics" : [ "BFS", "DFS", "Topological Sort" ]
        }
    ]
}


Compare JavaScript Object vs JSON:

  JavaScript Object JSON
1. Keys in key/value pairs don’t always need double quotes. Keys in key/value pairs need to be enclosed in double quotes.
2. It is only used by JavaScript. Other programming languages are able to generate and use JSON.
3. Functions are compatible with JavaScript Object. Functions are incompatible with JSON.
4. The built-in JavaScript JSON.stringify() method allows you to convert JavaScript objects to JSON format. Using the built-in JSON.parse() method, you can transform JSON data into a JavaScript object.

RELATED ARTICLES

Most Popular

Recent Comments