Explain the difference between object and Json in detail with example in Javascript?

Let's first understand what is Object:

In JavaScript, an object is a collection of properties, where a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the language, you can define your own objects.

Here is an example of an object in JavaScript: 
const car = {
  make: 'Toyota',
  model: 'Corolla',
  year: 2020,
  start: function() {
    console.log('Starting the car');
  }
};

In this example, the car object has four properties: make, model, year, and start. The make and model properties have string values, the year property has a number value, and the start property has a function value.

Now let's understand what is Object

On the other hand, JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript programming language, Standard ECMA-262 3rd Edition - December 1999. 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. These properties make JSON an ideal data-interchange language.

Here is the same example as above, but represented in JSON:
{
  "make": "Toyota",
  "model": "Corolla",
  "year": 2020,
  "start": "Starting the car"
}
As you can see, the JSON representation is very similar to the JavaScript object, but it is a string rather than a live object.

You can convert a JavaScript object to JSON using the JSON.stringify() method, and you can convert a JSON string to a JavaScript object using the JSON.parse() method.

Here is an example of converting a JavaScript object to a JSON string and back:
const car = {
  make: 'Toyota',
  model: 'Corolla',
  year: 2020,
  start: function() {
    console.log('Starting the car');
  }
};

const carJSON = JSON.stringify(car);
console.log(carJSON); // {"make":"Toyota","model":"Corolla","year":2020,"start":"Starting the car"}

const carObject = JSON.parse(carJSON);
console.log(carObject); // {make: "Toyota", model: "Corolla", year: 2020, start: "Starting the car"}

Summary:

In summary, a JavaScript object is a collection of properties that can contain any value, including functions, whereas JSON is a text-based format for representing JavaScript objects that is easy to read and write.


Different between Object and Json in javascript in detail


Another difference between JavaScript objects and JSON is that JSON doesn't support comments, whereas JavaScript objects do. This means that if you want to include comments in your data, you need to use a JavaScript object rather than JSON.

Here is an example of a JavaScript object with comments:
const person = {
  // The person's name
  name: 'John',
  // The person's age
  age: 30,
  // A method that says hello
  sayHello: function() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old`);
  }
};

If you tried to include comments in JSON, it would result in an error because the JSON parser doesn't recognize them.
{
  // This is an error
  "name": "John",
  "age": 30,
  "sayHello": "Hello, my name is John and I am 30 years old"
}

Another difference between JavaScript objects and JSON is that JavaScript objects can have all kinds of values as property values, including other objects, whereas in JSON all property values must be strings, numbers, booleans, null, or other JSON objects. This means that if you want to include a complex value, such as an array or another object, in your data, you need to use a JavaScript object rather than JSON.

Here is an example of a JavaScript object with a nested object:
const person = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'New York',
    state: 'NY'
  }
};

If you tried to include a nested object in JSON, it would need to be converted to a string first.

{
  "name": "John",
  "age": 30,
  "address": "{\"street\":\"123 Main St\",\"city\":\"New York\",\"state\":\"NY\"}"
}

Conclusion:

In conclusion, while JavaScript objects and JSON are similar in many ways, they are not interchangeable. JavaScript objects are live values that can contain any value and can be modified, whereas JSON is a static representation of data that can only contain strings, numbers, booleans, null, and other JSON objects. It is important to understand the differences between these two formats in order to use them effectively in your code.




Please don't forget to like, share and comment if you like this post. 

Cheers :) 
Happy Coding...

Post a Comment

Previous Post Next Post