DEV Community

Cover image for JavaScript Object vs JSON: Demystified
deji adesoga
deji adesoga

Posted on • Edited on

34 5

JavaScript Object vs JSON: Demystified

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.
{
"name":"Ben Halp",
"age":35,
"car": null,
"address":{
"street": "55, cathedral road",
"city": "Boston",
"cop" : true
},
"hobbies":["reading", "golfing"]
}
view raw data.json hosted with ❤ by GitHub

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.
{
"name":"Ben Halp",
"age":35,
"car": null,
"address":{
"street": "55, cathedral road",
"city": "Boston",
"cop" : true //Boolean
},
"hobbies":["reading", "golfing"] //Array
}
view raw data.json hosted with ❤ by GitHub

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.
let person = {
name : "Erik Wolfgang",
location : "Europe",
established : "1945"
}
view raw app.js hosted with ❤ by GitHub

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.
let member = {
name: "dave",
"amount": function() {
return 300000;
},
"city": "bristol"
};
view raw app.js hosted with ❤ by GitHub

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.
let data = { name:"Viktor", age:40, haircolor:"pink"}
let x = data;
x.age = 10; // This will change both x.age and data.age
view raw app.js hosted with ❤ by GitHub
  • javascript Objects has it's own object version of primitive data types.
let first = new Object(); // A new Object object
let second = new String(); // A new String object
let third = new Number(); // A new Number object
let fourth = new Boolean(); // A new Boolean object
let fifth = new Array(); // A new Array object
let sixth = new RegExp(); // A new RegExp object
let seventh = new Function(); // A new Function object
let eight = new Date(); // A new Date object
view raw app.js hosted with ❤ by GitHub

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.
// Bracket Notation
let person = {
name : "Kudvenkat",
location : "India",
age : 95
}
console.log(person['name']); // Output : Kudvenkat
//Dot Notation
let person = {
name : "Kudvenkat",
location : "India",
age : 95
}
console.log(person.name); // Output : Kudvenkat
view raw app.js hosted with ❤ by GitHub

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.

// JSON data in a javaScript variable
let data = '{"name": "Andre", "age": 19, "country": "Venezuela"}';
// Converting JSON string into a javaScript object
let obj = JSON.parse(data);
// Get Data From javaScript Object
alert(obj.name); // Output: Andre
alert(obj.age); // Output: 19
alert(obj.country); // Output: Venezuela
view raw app.js hosted with ❤ by GitHub

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.

// javaScript Object in a javaScript variable
let data = { name: "Perpetual", age: 26, city: "Lagos" };
// Converting javaScript Object into a JSON string
let obj = JSON.stringify(data);
// Display Data From a JSON string
alert(obj); // Output: {"name": "Perpetual","age": 26,"city": "Lagos"}
view raw app.js hosted with ❤ by GitHub

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

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay