In following example, a discussion of JSON compared to a JavaScript object, we arrive at the conclusion that JSON is nothing more than a stringified representation of the object. To understand its storage procedure conceptually, consider the following example:
let completeJSON = {
"hello": "World is a great place",
"num": 5
}
Let us divide this concept with respect to the major operations that are performed on a complete JSON. When I say complete JSON, I mean the whole structure and not its subset of key-value pairs. So the first operation is to serialize. Serialization is the process of removing blank spaces as well as escaping the internal inverted quotes (if any) so as to convert the whole structure into a single string. This can be illustrated as follows:
let stringifiedJSON = JSON.stringify(completeJSON);
If you console.log the variable stringifiedJSON, we get the following output:
"{"hello":"World is a great place","num":5}"
In this case, the whole JSON is stored as a normal string. Let's represent it as follows:
In the preceding conceptual block diagram, slot 1 of the memory location is represented by stringifiedJSON with a single input string value. The next type of storage might be parsed JSON. By using the previous snippet, if we parse the stringifiedJSON we get the output as follows:
let parsedJSON = JSON.parse(stringifiedJSON);
The output received will be :
{
"hello": "World is a great place",
"num": 5
}
In the preceding representation of JSON, it is clearly identified that the value received is not a string but an object; to be more precise, it is a JavaScript object notation. Now, the notion of storage of JSON in memory is the same as the JavaScript object.
Hence, in this case, the scenario is totally different. Let us visualize it as follows:
Now, let us derive some inferences after observing the memory representation:
- Object storage is not sequential; the memory slots are randomly selected.
In our case it's slots 4 and 7. - The data stored in each slot is a reference to the different memory location.
Let us call it an address.
So, considering our example, we have the fourth slot with the address object.hello. Now, this address is pointing to a different memory location. Assume that the location is the third slot, which is handled by the JavaScript execution context. Thus, the value of parsedJSON.hello is held by the third slot.