What is JSON? (JavaScript Object Notation)

Javascript Object Notation, or JSON for short, is a data structure that uses only human-readable text to transfer or store information. It is very popular, especially in the context of web technologies, for transferring data between applications.

JSON allows you to import and export data in a structured manner without doing a lot of work to parse it. Frequently when you interact with a REST API, you will pass JSON structures back and forth. An example of when JSON is used for storing data can be seen in TinyDB. TinyDB stores all the data on disk in the form of JSON. When you work with NOSQL implementations, they generally store the documents in your database as JSON.

How is JSON Structured?

Now that we are clear on what JSON is used for, let’s spend some time talking about how JSON is structured. JSON structure is very simple. It is simply a collection of Key/Value pairs enclosed with some curly braces. Here is an example of a JSON structure of People’s favorite colors:

As you can see, it is very simple. We have two related strings, separated by a colon, and enclosed in quotes. We separate each pair with a comma. We wrap the entire thing in curly braces.

In the above example, we only used strings. However, JSON supports other data types as well. For example, you can use numbers:

Or, you can use arrays:

Finally, you can also nest JSON objects. Here is a JSON document showing everyone’s categorized list of favorite foods

In the above example, we have a JSON object like this:

But, it is inside a larger JSON document.

In JSON, whitespace does not matter. So we can add line-breaks and extra spaces where we want to make the documents easier to read. We can take our food list example, and re-structure it again:

This example is easier to read in some ways, but it does take a lot more space on the screen. It is really a personal preference on how you want to write it out.

In the next section, we will show code examples, we will discuss how to write and parse these different JSON structures.

How do I use JSON?

In Python, it is very easy to use JSON. Let’s start by using our first example where we listed everyone’s favorite colors. We first import the JSON module a variable and declare a variable to hold our JSON structure:

When we use the json.loads function and pass in our json, it is converted into a dictionary, which makes it easy to lookup what color everyone likes

The following code will print out Green, Blue, and Red:

Next let’s move on to our more complicated JSON structure and look at how we can query a JSON structure with Nested JSON objects.

At first, everything starts off the same:

But you can see that when we access the nested values, we have to add one more layer. First we reference the person’s name, then the field from the nested document we wish to access. Below, we will print out Bob’s favorite vegetable, Joe’s favorite dessert, and Sally’s favorite bread:

Something else to note about the JSON object referenced above. Notice it is all one a single line. Earlier I said that JSON does not care how you structure it. And if you are importing it from a file, or receiving as response from a REST endpoint, it does not matter. However, Python cares a lot about spacing and formatting. So it is much easier to keep it all on one line when you are statically setting it in your document. That is not the typical way you work with JSON objects.

At some point Bob might change his mind about his favorite vegetable. Today, Bob no longer likes Broccoli, instead he prefers to each spinach. You can easily update the dictionary in a similar fashion to when you queried it to find out Bob’s favorite vegetable:

After running the above code, the JSON has been updated to show that Bob’s favorite vegetable is spinach. You can run the print command again to confirm:

Next we need to track another person’s preferences. To add another key to our JSON structure, you simply call out the key and set a value:

As you have seen, working with JSON files is almost the same as working with any other collection/container in Python. After converting the JSON string into a dictionary, you can perform all of the various dictionary functions on that object. For more information on working with Python Collections, see our article on Python Collections.

Importing and Exporting JSON files

At this stage, we have our JSON document all nicely constructed, and we want to save it to disk so we can import it next time we run our application. If we start with the above example, you can add the following code to write your json to disk:

Notice we call the json.dumps function and pass in the parsed_json dictionary. This json.dumps function is convert the dictionary back into JSON before we write it to disk. The result is you will have a text file called mydata.json containing everything in your JSON structure:

Screenshot showing contents of JSON file

Next time your application starts up, you need to re-load your data into your application. When you read your data, it is a lot like our examples from earlier. You have a bunch of text that you need to parse and ultimately turn into a dictionary. You can accomplish this with the code below:

You can see we open the file, read it into the json_data variable, close the file, then parse the json_data variable. The result is we once again have a dictionary we can query for any of the JSON data we wish to access.

Summary

Today we have discussed how to read and write JSON files. How to lookup and update data stored in JSON documents. And we have looked at some of the things JSON is used for.

Below you can find all of the code examples from above consolidated into one place:

%d bloggers like this: