Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
JavaScript and JSON Essentials

You're reading from   JavaScript and JSON Essentials Build light weight, scalable, and faster web applications with the power of JSON

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher
ISBN-13 9781788624701
Length 226 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Sai S Sriparasa Sai S Sriparasa
Author Profile Icon Sai S Sriparasa
Sai S Sriparasa
Bruno Joseph D'mello Bruno Joseph D'mello
Author Profile Icon Bruno Joseph D'mello
Bruno Joseph D'mello
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with JSON 2. The JSON Structures FREE CHAPTER 3. AJAX Requests with JSON 4. Cross-Domain Asynchronous Requests 5. Debugging JSON 6. Building the Carousel Application 7. Alternate Implementations of JSON 8. Introduction to hapi.js 9. Storing JSON Documents in MongoDB 10. Configuring the Task Runner Using JSON 11. JSON for Real-Time and Distributed Data 12. Case Studies in JSON 13. Other Books You May Enjoy

How is JSON stored in memory?

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 serializeSerialization 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.

Assume that we are using a JavaScript engine to interpret code.

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime