DEV Community

Cover image for JavaScript Objects vs JSON: Are they same?
Swapnoneel Saha
Swapnoneel Saha

Posted on • Edited on

JavaScript Objects vs JSON: Are they same?

Introduction

While learning JavaScript, you will find about JavaScript objects, which may seem familiar to you if you are coming from other programming languages like Java or Python. But soon enough, you will also find something called JSON or JavaScript Object Notation which is widely used all over web development. You may find a lot of similarities between these two and may think about whether they are the same thing or not!!

So in this blog, I'm here to clear your confusion regarding JS Objects and JSON. So let's dive in!!

What is JavaScript Object?

JavaScript Objects are the fundamental constructs which are used to store collections of data, entities or some specific values. They are essentially containers for named values, known as properties, and functions, which are termed methods when they are part of an object.

Compare it with a cup, for example, a cup is an object, with properties. It 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. We can define an object in JavaScript like this:

const cup = {
    color : "black",
    design : "cylindrical",
    weight : 300,
    material : "glass"
}
Enter fullscreen mode Exit fullscreen mode

What is JSON?

JSON, which stands for JavaScript Object Notation is a light-weight data interchange format that is both easy for humans to read, write and understand, and also convenient for the machines to parse and generate.

It's completely language-independent but uses the conventions/syntax that are similar to that of the C-family of languages, like C, C++, C#, Java, JavaScript, Python, and many others.

And that's why, we use JSON primarily in data transmission between a server and a web application or as a format for storing configuration or the state information. The simplicity and ease of use of JSON have made it a popular choice for web APIs and configuration files. Here's an example of JSON data:

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 30,
    "isEmployed": true,
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "zipCode": "12345"
    },
    "phoneNumbers": [
        {"type": "home", "number": "212 555-1234"},
        {"type": "office", "number": "646 555-5678"}
    ]
}
Enter fullscreen mode Exit fullscreen mode

What's the difference?

It might have been already clear to you how JS Objects and JSON have a lot of dissimilarities between them. But to further develop on that, here are some more aspects where they differ from each other:

JavaScript Objects JSON
Flexibility It can contain functions and support methods that can manipulate the object's data. It is purely a data format and cannot hold functions.
Syntax Objects can be declared using the object literal syntax, constructor functions, or the class keyword starting from ES6. JSON closely resembles JavaScript object literal syntax, but it has stricter rules. For example, property names must be double-quoted, and trailing commas are not allowed.
Purpose They are used within JavaScript code to organize data and functionality. It's a language-independent text format that is used to transmit data between a server and web application or as a file format.

Conclusion

Understanding these differences is crucial for effectively using JavaScript objects within your code and JSON for data interchange between clients and servers or storing the configuration and state. And, I hope you folks have enriched yourself today with some known or unknown concepts today and cleared any misunderstanding regarding these topics. I wish you a great day ahead and till then keep learning and keep exploring!!

Also, if you want to connect with me, check out my Twitter/X profile, where I regularly post Tech contents: https://x.com/swapnoneel123

Top comments (2)

Collapse
 
ddebajyati profile image
Debajyati Dey

You destroyed my confusion. Thanks.

Collapse
 
swapnoneel123 profile image
Swapnoneel Saha

It's great that you found it helpful!!