JSON (JavaScript Object Notation), as defined on MDN Web Docs, “(…) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa)”.
In this post, I’ll show you how you can parse the content of a JSON file, using a Python script, and display it on your terminal.
In this example, we’ll use a JSON file called data.json where we stored a list of users and some information about them.
data.json
{
"users": [
{
"id": 0,
"name": "Ruben",
"role": "Administrator"
},
{
"id": 1,
"name": "Filipa",
"role": "Administrator"
},
{
"id": 2,
"name": "Rodrigo",
"role": "User"
}
]
}
Now, the actual code 🙂
read-data.py
In order to work with JSON files, we’ll use the json library. So, the first thing that we need to do is to import that library.
# Import libraries
import json
Than, what we need to read the file and load the file content into a variable.
# Read JSON file
json_file = open('data.json', 'r')
json_file_data = json.load(json_file)
At this moment, json_file_data variable contains the following data:
# Print raw data
print(f"RAW DATA: {json_file_data}")

Now, we just need to loop through the list of users that we’ve loaded and print the user data.
# Loop through the list of users and print data
for user in json_file_data['users']:
print(f"ID: {user['id']}")
print(f"NAME: {user['name']}")
print(f"ROLE: {user['role']}")

Here’s the how final file looks.
# Import libraries
import json
# Read JSON file
json_file = open('data.json', 'r')
json_file_data = json.load(json_file)
# Loop through the list of users and print data
for user in json_file_data['users']:
print(f"ID: {user['id']}")
print(f"NAME: {user['name']}")
print(f"ROLE: {user['role']}")
And that’s it! I hope you find this post useful.
See you on the next one 🙂