What is a JSON file and what are Python JSON files used for?

JavaScript Object Notation is a standardized format used widely on the web especially with APIs to transfer data as text over a network.

Fortunately, Python has a built-in package for working with JSON data. All that is needed of us is to import the package into our current working space whenever we want to work with JSON data.

Using this package we can manipulate the data and use it in any way that we so wish.

 

 

JSON’s format is similar to that of a python dictionary or a list and whenever a JSON file is imported into a python script, the JSON file is automatically turned into a Python dictionary.

So to get started with using JSON we need to first import the JSON module at the top of the file that we are working on. We do not need to install any other thing. 

import json

Python JSON file handling example 1

Now, to open the JSON file we are going to use the context manager that lets us manage resources such as files and database connections in an easier way. In this case, we are going to open a sample JSON file.

Opening files using the context manager in python is considered to be one of the best ways to handle files of any format including those of other formats such as CSV and even text.

One more advantage of using the context manager is that we do not need to worry about closing files since the context manager handles that for us.

 

 

In the scenario above we have used json.load instead of json.loads since the former is useful when opening a file when the intention is to load the contents of that file while the latter is useful when loading contents of a string.

Suppose that we have downloaded some data from an object or a variable, in this case, we would want to use json.loads instead of json.load.

Now if we go ahead and print out variable data that holds the JSON file we should get the exact JSON file that is stored in the sample.json file.

 

python json

Python JSON file handling example 2

Now if we go ahead to print out the type of data object that is storing the JSON file, we will definitely find out that it is a dictionary. This is because the JSON module that we imported at the top of our file has turned the JSON file into a dictionary.

 

Python JSON file handling example 3

Since this is a dictionary we can start accessing some of the keys and thus their values. We are going to create a variable ‘a’ that is going to store the values that we’re accessing from the JSON object that gives us the dictionary.

For instance, we access the value of the title key by simply calling our glossary and then chaining together the key of the title. So when we run this we will definitely get the value of the key.

 

 

However, let us say that weren’t interested in any bit of the above information but we want to get the glossEntry dictionary including the list that is within it.

All that we need to do is follow the JSON tree and chain together the dictionaries until we get to the dictionary that we want to access, we first have the title under the glossary.

Since that is not the section that we are interested in we will therefore proceed to the glossDiv herein is the dictionary that we are interested in.

Although it is named GlossList by cross-examining its content it is clear that it is a dictionary and not a list.

 

python json

Python JSON file handling example 4

Now to retrieve the contents of the dictionary we still need to call the keys, now that we have reached the dictionary glossEntry whose contents we have all that we need to do is specify the key of the dictionary that we want to access.

However, if we do not specify any key from the GlossEntry Dictionary then printing the following code will output all key-value pairs in the GlossEntry dictionary. As shown in the code below.

 

Python JSON file handling example 5

Now we can also access the part of the GlossEntry dictionary by chaining to the tree the part of the dictionary that we want to access. For instance, we can access the GlossDef dictionary that is nested inside the GlossEntry dictionary.

 

Python JSON file handling example 6

We can also go deeper into the file and access the elements of the list using their respective index numbers. Since Python is a zero-based language the first element of a list is always represented by index zero. 

 

 

We have worked our way through all the bits of information in this sample.json file all the way down to its deepest part.

On most occasions when working with JSON data we won’t have to chain a lot of stuff, nevertheless understanding how to use JSON data is an important skill especially when dealing with Web Content, APIs or rendering data to a web page together with JavaScript.

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.