Nowadays, many data sources that our applications consume come in this format. I’m going to explain how to get data from a JSON file with Python.
First of all, we’re going to generate a JSON file to read later with our Python program. In my case, it’s based on a set of classes, each with a series of properties. We’ll save this file in the same directory where we create the Python file. I’ve named it Entities.json and you can find it here.
Now, let’s move on to the programming part. The first thing we need to do is create a new Python file called Reader.py. We’ll import the package that will give our code the ability to read JSON files with Python. We do it as follows:
Next, we open the JSON file we created earlier, read it, and load its contents.
f = open("Entities.json", "r") content = f.read() jsondecoded = json.loads(content)
Let’s go over each instruction:
- The “open” function is a fundamental instruction in Python that allows us to access and open a specific file. In the first parameter, we need to specify the file’s location by entering its corresponding path. In the second parameter, we specify how we want to open the file. In this case, we will open it in read mode, which is why we use “r”. There are various opening modes available.
- The second instruction allows us to perform an action on the file we previously opened. In this case, we are only interested in reading the file’s contents without making any changes, so we execute the “read()” action. We store the data read from the file in the variable “content”.
- Finally, we need to decode the JSON data so that we can utilize it in our application. Therefore, we call the JSON package and instruct it to execute the “loads()” function on the content we previously read. Now, we have the JSON content in memory and can work with it.
To extract the data from the JSON and display it through the terminal, we will iterate through each of the classes specified in the document and display the name of each entity that comprises our data set. We can accomplish this by adding the following block of code:
for entity in jsondecoded["Classes"]: print(entity)
With this block, we iterate through each element of the “Classes” subset. The name in brackets has to be the name assigned to the tag of that subset. You can check in my JSON that the name of the first subset is the same as the one specified in this loop. The complete JSON will be displayed on the console but without format.
As you may have guessed, this is illegible and, in most cases, useless. Let’s see how to specify the data we’re looking for. We update the loop with this new version:
for entity in jsondecoded["Classes"]: entityName = entity["Name"] print(entityName)
This time, we extracted the value with the “Name” tag of each entity. It should look something like this:
Much better, right? Now that we know how to extract elements given the name of their tags, let’s try to obtain the value of the elements that are deeper in the hierarchy, such as the properties of each of the entities. Everything is specified in the JSON from the beginning. We’re going to make some changes to the code block.
for entity in jsondecoded["Classes"]: print("Entidad " + entity["Name"] + ". Sus propiedades son: ") for entityProperty in entity["Properties"]: print("Propiedad " + entityProperty["Name"] + " de tipo " + entityProperty["Type"])
As you can see, for each entity, we iterate through each of its properties and display them. Note that the name of the tags matches the one specified in the JSON. If we run this, we should get something like this:
Entity Actor. Its properties are: Property name of type String Property middleName of type String Property surname of type String Property photo of type String Property email of type String Property phone of type String Property address of type String Entity Message. Its properties are: Property sendDate of type Date Property subject of type String Property body of type String Property topic of type String
To access the rest of the elements and hierarchies, you’ll need to follow the steps we’ve seen.