loading...
Cover image for JavaScript Object vs JSON: Demystified

JavaScript Object vs JSON: Demystified

desoga profile image deji adesoga Updated on ・4 min read

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.

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.

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.

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.

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.
  • javascript Objects has it's own object version of primitive data types.

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.

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.

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.

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:
here

Posted on Oct 2 '19 by:

desoga profile

deji adesoga

@desoga

One Half of a Full Stack Developer. Owner of The Code Angle.

Discussion

markdown guide
 

Thanks for the great post.

You can use JavaScript / JSON Converter to convert between JavaScript objects and JSON data on the browser. I hope anyone working with JavaScript and JSON will find this useful.