This 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?
This 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?
"Can be considered an Object and also a Json? If not, why not?"
Because javascript
objects exist since the language was created, and JSON
is a way of representing the objects and was created later, in text format, that is used to transfer those objects, for example executing a service. It does not necessarily have 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 of representing 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 instantiating?"
There are structures on objects that can not 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: [1, , 2]
.
You can read more here in this other question: what is json for what it is 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
JSON is a notation. In this case, it is geared towards serializing and deserializing information.
It is not an object in the sense of having behaviors, it is more a crude structure. It would be something like struct
of C: just a dataset.
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 either variables, methods, or 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.
JSON is text, it is a String .
It is compatible with JavaScript because it was created using the syntax of this 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 itself 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"