Json Is Not Javascript Object.

Json Is Not Javascript Object.

Hi guys!

Oftentimes, developers confuse JSON for Javascript objects because they appear similar. This post is specifically written to distinguish between JSON and javascript objects. Their similarity does not mean they can be used interchangeably.

Let clarify first what (JSON) is.

JSON stands for JavaScript Object Notation. It is language-independent, though it is derived from JavaScript, it can be used by other programming languages like python, ruby, PHP, C just to mention a few.

JSON has a self-describing property, my last article explains why JSON data should be readable. JSON is a data exchange format, the data is represented in text and lightweight, therefore is easy to share between devices.

JSON is syntactically represented using the following pattern; JSON is represented in a key and value pair pattern. JSON KEY and VALUE. The key must be a string, the value can be string, number, array, object, boolean and null.

JSON SYNTAX RULE Curly braces hold objects {}. Square brackets hold arrays []. Data is separated by commas , . Keys and values are enclosed in a double quote "", demarcated by a colon :

Example of JSON data:

// JSON key-value pair

const person = {
   "firstName": "Adesanya",
   "lastName" : "Bolarin",
   "Nationality"  : "Nigeria",
   "Single": true,
   "Age":23,
   "Address":[ 
      {"LGA" : "Akure south","State" : "Ondo State"},
      {"LGA" : "Alimosho","State" : "Lagos State"},
      {"LGA" : "Patani","State" : "Delta State"},
   ]
 }
 console.log(person);

The left-hand side of the code above is the key, while the right-hand side is the value.

In contrast, Javascript Object.

Javascript object is a combination of an object, set of properties, methods as action to be implemented on an object. A person as an object has a property of colour, height, weight, nationality and age, it means attribute. the person can perform actions like sleeping, crying, reading, eating, and writing.

The property is represented in key and value pairs. The value of an object key can be a function. An object is housed in a curly bracket. Value is enclosed in single or double quotes. An object can be accessed using a dot or a bracket notation.

Example of object data:

// key value pair :object
const person = {
  firstName: "Ajayi",
  lastName : "Bolarinwa",
  Nationality  : "Nigeria",
  // job: "frontend developer"
  Age : 23,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
}

console.log(person.lastName)
console.log(person.fullName());

The major distinctions between JavaScript object and JSON are:

//value enclosed in quotes JAVASCRIPT OBJECT

const name = {
   firstName: "Molly ",
   lastName : "Buzzy",
}
//Keys and values enclosed in quotes JSON

 let name = {
   "firstName": "Molly",
   "lastName" : "Buzzy"
 }

Javascript object can take a function but JSON can not. JSON does not allow commenting in its code, while Javascript object allows commenting. JSON must be wrapped double quote, and specifically its keys while javascript object cannot enclose its keys in quotes. Look out for this in code samples or snippets.

Thank you for reading this far, I hope you found it helpful.
I’ll be glad to hear you out in the comment section.

Did you find this article valuable?

Support BeeC00des by becoming a sponsor. Any amount is appreciated!