- What is a JSON file and what are Python JSON files used for?
- Python JSON file handling example 1
- Python JSON file handling example 2
- Python JSON file handling example 3
- Python JSON file handling example 4
- Python JSON file handling example 5
- Python JSON file handling example 6
- Summary
- Programming tutorials
- Learn to code and change your career!
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 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 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.
Summary
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.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
