JavaScript Object vs JSON: Demystified
Tags: javascript, webdev, beginners, json
Published: 2019-10-02 08:58:59

---
title: JavaScript Object vs JSON: Demystified
published: true
description: 
tags: javascript, webdev, beginners, json
cover_image: https://res.cloudinary.com/dz4tt9omp/image/upload/v1569800918/javascript_json.jpg
---

> _I'd be more frightened by not using whatever abilities I'd been given. I'd be more frightened by procrastination and laziness.  -Denzel Washington_


## Introduction

The goal of this article is to clarify the differences between **javascript Objects** and **javaScript Object Notation**(JSON). As beginners, while working with **JSON** data it is quite possible to confuse it with **javascript Objects**. While **JSON** cuts across different programming languages, **javascript objects** are only peculiar to javascript.

Because of the similarities between **javaScript Objects** and **JSON**, it is possible to use a javascript program to convert **JSON** data into native **JavaScript Objects** and vice versa.


## So, What is JSON?


**javascript Object Notation**(JSON) is a lightweight format for storing and transporting data. It is often used when data is sent from a server to a web page, it is also self-describing and easy to understand. 

**Douglas Crockford** originally specified the **JSON** format in the early 2000s.

Since the **JSON** format is text only, it can easily be sent to and from a server, and used as a data format by any programming language.

A subset of javascript, **JSON** is virtually supported by all modern programming languages in one form or another. 

The structure of **JSON** is based on two basic structures, 

- A collection of key / value pairs.

- An ordered list of values.

Also, there are six data types allowed in *JSON*, and they include:

- Array

- Boolean

- Null

- Number

- Object

- String



## What are javaScript Objects?

**javaScript Objects** are used to store a collection of data. All JavaScript values, except primitive data types(Number, String, Boolean, null, undefined and symbol), are objects.

**javaScript Objects** are a bit more complex in the sense that, they may contain a combination of primitive data types. They are contained in curly braces {…}, and can be  created by object literals, object constructor syntax, constructors and prototypes. 


In it's basic form, **javaScript Objects** are **key:value** pairs, whereby a key is called a string, and a value can be called anything.



## Features / Characteristics / Syntax of JSON


* In **JSON**, the six data types which are supported will take their various forms.


{% gist https://gist.github.com/desoga10/5af778236b28a121d5f3c8ac01b72813 %}


From the above example, the *name* is set as string which has a double quote for both the key and the value, *age* is set as number, the *address* is included in an object, in the *address* object, the key *cop* is set as a boolean, *car* is null and *hobbies* is set as an array.

 
* In **JSON**, it is strictly forbidden to make use of comments inside the file.


{% gist https://gist.github.com/desoga10/401707c83e75abd4c1ee26be6d1fb6fd %}


The above code in the **data.json** file will give us an error because of the comment in the code.


* It is also worthy to note that **JSON** is supported by more than 50 languages including JavaScript, Perl, Java, Python, Ruby, php, C.


* Functions or methods are not allowed in **JSON** as they only support data format.


## Features / Characteristics / Syntax of javaScript Objects


* Keys are not needed to be covered in double quotes and only the values are required to be covered in double quotes.


{% gist https://gist.github.com/desoga10/3a7448518e12d9bac0706c58f715f17d %}


This makes it easier to differentiate **javaScript Objects** from **JSON**.


* Also, a **javascript Object** may also contain a function as one of it's members.


{% gist https://gist.github.com/desoga10/b9f16248478be5ec67888118a6cdccb9 %}


From the above code, **amount** is a method of the member object, which contains data that is stored in the object property.


* javaScript Objects are mutable in nature. That is, they are objects whose state can be modified after they have been created.


{% gist https://gist.github.com/desoga10/e24b0615046e52a1a440e96095f2233c %}

* javascript Objects has it's own object version of primitive data types.


{% gist https://gist.github.com/desoga10/8238b3e901c88984a4de94e1d3911927 %}


It is better to use primitive data types (string, number, boolean, null, undefined, symbol), as they are faster and not complex.


* The members of a javascript Objects can be accessed in two ways: **The Bracket Notation** and **The Dot Notation**.

{% gist https://gist.github.com/desoga10/ac92054688f2e324673c4f1f1409f27c %}



## Conversion of JSON Into javaScript Object

 
**JSON** can be converted to a javascript Object by using the **JSON.parse()** function. It is worthy to note that, if the data retrieved either from a file or a server is not in a **JSON** format, an error will occur.

{% gist https://gist.github.com/desoga10/b95320b5eaaf7a5de5061d23d5b89f93 %}

From the example above, we were able to use the **JSON.parse()** function to convert a **JSON** string into a javascript Object, and also access the values of each string using the dot notation we spoke about earlier.


## Conversion of javaScript Object Into JSON 

**javascript Object** can also be converted into **JSON** using the **JSON.stringify()** method. 


 {% gist https://gist.github.com/desoga10/74b05345588251049032c75c61b69b25 %}

**obj** is now a string. Also **JSON.stringify()** is supported by all major browsers. 


## Conclusion

In javaScript, almost everything is an object. So it is important for one to get very familiar with them. Additionally, Objects gives us the ability to read our code more clearly and also helps with better data representation.

With the help of this article, I believe you should be able to identify the major differences between **javascript Object** and **JSON** at first glance.  


To get more free content on web development, subscribe to my newsletter:
<a href="http://eepurl.com/gZ-jJH" target="_blank">here</a>