In the last post, we took a look at a Todo application. Our Todo app was very simple and it has no means to store data. To be able to store data we need to learn to use JSON. In this post, we are going to talk about the JavaScript Object Notation or JSON for short.

What is JSON? JSON or the JavaScript Object Notation is a way for us to store information in an organized and easy to access manner. Quite simply, it gives us a method of storing data in a readable format that is also very logical as well. From the name, JavaScript Object Notation, we can infer that this method relies heavily on JavaScript Objects. Here is a simple example in JSON format.

We’ve created an object here that we can access with the variable person. We’ve used the object literal notation to define the variable as an object. Inside of this object, we can have any number of "name" : "value” pairs separated by commas. When we want to access the date inside of this object we can simply refer to the name of the property we need using the standard “dot syntax”.

Nested Arrays and Objects

We can also store more objects into one single variable by taking advantage of arrays. For example, if we want to have two Person objects in one variable expression we can simply enclose them in square brackets and separate the two objects with a comma. Another way to store multiple people in one variable is through the use of nested object notation. Here is an example of both.

Using nested objects to store data makes use of much easier to understand syntax than using arrays. As you can see, we just simply use the dot syntax and call the outer object name, the inner object name then the property name that we wish to access.

The great thing about JSON or the JavaScript Object Notation is that it is lightweight and also is very readable. It also makes use of the JavaScript language which we are currently working with. We don’t have to import SQL or another database language to give our applications a backend. While this is true, it is worth noting that JSON isn’t all technically JavaScript. JSON just happens to be syntactically identical to the code for creating JavaScript Objects.

Language Independence

JavaScript Object NotationJSON the JavaScript Object Notation typically is defined in its own file, a .json file. If you build a node application, you will use JSON to store your dependencies and project information. Also, because of it’s easy of use, JSON is used as a language independent way to store data for applications. This means, that you will often see JSON in Ruby, Python, Perl, and PhP applications to name a few examples. The standard boilerplate node package.json file looks something like this.

Closing

Today, we took a brief look at JSON or the JavaScript Object Notation. We talked about how to create JSON, what it is used for and why it is useful. In our up and coming tutorials, we will look at some more advanced JavaScript techniques through the use of examples.