JSON VS JavaScript Object

JavaScript , , 0 Comments

In this article, we will talk about JSON VS JavaScript object. I’m hoping you already worked with both of them and when you know more about them you will automatically get what is the difference between them. But, there is a little story about why I’m writing about JSON VS JavaScript Object below:

Why I’m writing this?

If you don’t like short stories please skip to the next heading 🙂

I’m writing this because one day my boss was searching for sorting an array (yes he also code) and I asked, Which type of array do you want to sort?

He: It’s not easy, I’m searching for that.
Me: Try Me! (And I was thinking:- for sure you are not working on quantum object sorting)
He: It’s a JSON array!
Me: There is no such thing in JavaScript (I said that because I immediately guessed that he is talking about an array of Objects for sure.)
He: Yes, It’s an array of JSON.
Me: You mean an array of objects Right? (And thinking:- I never sort an array of JSON in my career.)
He: They are same.
Me: (that statement blows my mind) No, they are different!
He: They are same technically!
Me: (thinking no they are not but, he’s the boss) So, I said OK.
Then, I told him how he can sort an array of Objects. (an array of JSON. LOL!!) but he is a good programmer anyway.

The next day my colleague (a backend developer) asked me the same how is JSON is not a JavaScript Object. And it is a little hard to make him understand because of his too much logical thinking about everything.
So I explained to him and told him a couple of examples of how it is different.

Then he said, OK, but these are not a major difference? And I was like what are you? A machine learning algorithm that needs a minimum 70-80% match to believe! or whatever. But it is really good for me to explain something to him deeply.

So, I explain to him and showed some more examples then he believed.
Then I thought about writing this.

So, What is JSON anyway?

JSON (JavaScript Object Notation) is a lightweight format easy to read and write for humans. Easy to parse and generate for machines.

  1. JSON stands for (JavaScript Object Notation) even though it has JavaScript in its name we don’t need JavaScript knowledge to use it.
  2. Language Independent:- which means all major programming languages know how to handle JSON data. It is easy to get data out of it and convert their data to JSON.
  3. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. 
  4. Lightweight data-interchange format:- As I said before it is a lightweight data-interchange format that means we can use it to send and receive data between the server and our app (front-end) more faster because it is lightweight.

Why does it look like a JavaScript Object?

Because It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition – December 1999

There are two main structures that JSON build on:

  1. A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

JSON Example

How JSON look like?

{
  "name":"Aryan Mann",
  "current website":"https://www.buginit.com/",
  "favorite game":{
    "name":"Crash Bandicoot",
    "developer":"Naughty Dog",
    "scores":[91,93,-120,97]
  }
}

What is JavaScript Object?

Well, If you are a JavaScript programmer you know what it is and till now you have got an idea what is the difference between them. But anyway, I’ll show you some of them below.

As MDN said…

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.

source mdn

JSON VS JavaScript Object

There are six data types that JSON supports are the following:

But, JavaScript Object support any data type that JavaScript (language) itself supports as a value of its properties.

JavaScript Supported Data types below:

  • Boolean
  • Null
  • Undefined
  • Number
  • BigInt
  • String
  • Symbol
  • Object

# If you noticed JSON doesn’t support undefined but, JavaScript Object does. Instead of undefined JSON supports null.

# One more thing is you can assign a function as a value in JavaScript Object but not in JSON for example below:

var domain = {
    name: "buginit",
    extension: ".com",
    getFullDomain: function (){
        return this.name + this.extension;
    }
}

domain.getFullDomain()
// expected output: "buginit.com"

# You can’t comment inside JSON. It just doesn’t support comment, it must be all JSON data, but you can comment inside JavaScript Object.

Conclusion

JSON may look similar to JavaScript object but it is way different than the JavaScript object. If you read the whole article or the what is the JSON section you have an idea of how they are different from each other.

That’s all, If I have missed anything else please let me know. Share it with love.

Spread the love