Why are Javascript objects not always JSON?

Asked

Viewed 129 times

5

For example:

{
   "nome": "Meu nome",
   "sobrenome": "Meu sobrenome"
}

Can it be considered an Object and also a Json? If not, why not? What will differentiate one from the other is when instantiating?

3 answers

10

JSON is a notation. In this case, it aims to serialize and deserialize information.

It is not an object in the sense of possessing behaviors, it is more a raw structure. It would be something similar to struct C: only one data set.

In Javascript, an object also contains behavior and reference to variables. Since JSON is a given, it only contains constants, so it has no structure to store neither variables nor methods/functions.

And yes, the example you provided, because it contains only constants, can be characterized as JSON. And obviously it’s also a Javascript object.

9

JSON is text, is a String.
It is compatible with Javascript because it was created using the syntax of that language, but for a JSON to become an object it has to be interpreted ("Parsed").

Not all Objects can be transformed into JSON. Functions and references to parts of themselves are not allowed in JSON. All JSON strings can be interpreted to generate an Object.

const json = `
  {
   "nome": "Meu nome",
   "sobrenome": "Meu sobrenome"
  }
`;

const obj = JSON.parse(json);
console.log(json.nome); // udefined
console.log(obj.nome); // "Meu nome" 

9


"Can it be considered an Object and also a Json? If not, why not?"
Why objects javascript exist since the language was created, and the JSON is a way to represent objects and was created later, in text format, which is used to transfer these objects, for example by running a service. Even it does not necessarily need to be a Javascript object, it can be converted to an object in another language.

That is, an object is one thing, JSON is a way to represent it only. So much so that to work with or JSON as an object, it is necessary to convert it to an object.

"What will differentiate one from the other is when instantiating?"
There are structures in objects that cannot be converted to JSON, so not every object can be converted to JSON.

An example is that JSON does not allow empty elements in array as Javascript allows, for example like this: [1, , 2].

You can read more here on this other question: what is json that serves and how it works

Here’s a great OS link about the limitations of converting a javascript object to JSON: are all json Objects also Valid javascript Objects

  • 1

    On time, your answer came at a great time </pun>. I could tell by the links you placed directly from the browser the links to the questions. If you care about your "impact on the community," this is not the most efficient way. When you click on the "share" button, the system generates a link in the format /q/{qid}/{uid}, where the qid is the id of the issue and uid is the id of the user you shared. Anyway, I like to be aware of how much I’m impacting, so I consider this important.

  • Good pun :) I saw that you edited and stayed with, I had removed from the description the dns but was a little "dirty" the link at the end, besides not being by sharing, was much better even

Browser other questions tagged

You are not signed in. Login or sign up in order to post.