Home Python How to use JSON in Python to access GitHub API

How to use JSON in Python to access GitHub API

JSON is a widely used syntax for storing and transferring data over networks. They are used in web APIs provided by both small scale and large scale websites. In this tutorial, we will see the practical use of JSON in python for accessing an API provided by GitHub to retrieve the repository data.

It is recommended to look at the JSON tutorial before following this tutorial to have details of every line of code we will be writing.

Using JSON to access GitHub API

Introduction

GitHub is an open-source platform to host our source code for free and access version control tools like git. It helps developers easily contribute to open source programs. It’s also a great useful tool for team management projects.

An API is a web service provided by many websites to give data to users and developers. This is helpful for users to analyze the data and use those data in building useful applications. GitHub provides a useful web API that can be used to access useful information about users, user code repositories, etc.

This tutorial will build a command-line application that will prompt the user to enter a GitHub username and repository name and display some information about that repository.  To build the application, we will be using two libraries. We will use the request library to sent HTTPS requests to Github API and use the JSON library to work with the JSON data, which will be returned by the API.

Requirements

We don’t need to have any major installation to follow this tutorial. The JSON module is present in the python standard library, so it is installed when installing python in your system. We need to install the requests module, which we will use to send HTTPS requests to GitHub API. We can easily install the requests library using the pip package manager of python. We only need to type the following command in the terminal.

For Linux Users,

pip3 install -U requests

For Windows users,

pip install -U requests

This will install the latest version of the requested library in your system. If you don’t have pip installed in your system or have a problem working with it, you can refer to this tutorial.

Building the application

Now we shall start building our application. You can do many things using the GitHub API. GitHub also provided good documentation about their API that explains what things we can do with it. For this tutorial, we will make a program that will ask the user for the username and repository name and print some of the information about that repository. So let us start writing code.

Writing The Code

It is recommended to install an IDE for writing the python code. Here, I am using the Opensource VS code provided by Microsoft. You can use it or choose any of the available IDEs.

First of all, we need to import the required modules in our python application. Fire up your python IDE and write the following code on it.

import requests
import json

In the above code lines, we were importing the requests and JSON module in our program file.

After importing the required modules, we will create two variables for storing the username and repository name, which will be input by the user. I will give the variables a demo username Django and repository name Django for the Django web framework present in GitHub. See the below code for illustration.

username = 'django'
repo = 'django'

After declaring the variables, we need to create a URL in the format of Github API for that repository. The GitHub API URL for repository information looks at something like https://api.github.com/repos/{username}/{repositoryname}. In our case, the username and the repository name are Django, so that the URL will become https://api.github.com/repos/django/django. On clicking the above links, you can see that a new webpage has opened with some JSON data. These are the data provided by the Github API about the Django repository, which we requested. To build a URL like this in python, we will use the f-string. See the code below.

url = f'https://api.github.com/repos/{username}/{repo}'
print(url)

In the above code, we use the f-strings placeholder property to join variables with string directly. See, the variables are under curly braces. Running the code, we wrote until now will give us the output shown in the below image.

building the url for github api call

building the URL for GitHub API call

After building the URL for API, we need to use the requests module to send HTTP requests to the Github server for sending us the JSON data of the Django repository. To achieve this, we need to write the following line of code.

r = requests.get(url)

After doing this, we will get a response from the Github server, and if everything is OK, we will also get the data. Else we may get some errors like 404, 503, etc. To look at the data returned by the Github API, we can display it in the terminal by writing the following code.

print(r.text)

The above will print the JSON data returned by the Github API into the terminal. It may look like something, as shown in the below image.

returned json data

returned JSON data

We have got the data about the Django repository, but we don’t need all the data. We need only some information like when the repository was created, how many stars it got, etc. We will use the JSON module that we imported at our application’s first lines to achieve this. The JSON module can be used to convert the JSON data into a python dictionary, after which it will be easy to work with the data in python. To convert the JSON data into a python dictionary, we need to use the JSON module’s loads() function. See the below code for illustration.

data = json.loads(r.text)

This will convert the JSON data into a python dictionary. We can now access each data present in the python dictionary very easily using the dictionary’s keys in the index. See the below code.

print(data['name'])
print(data['description'])
print(data['created_at'])
print(data['language'])
print(data['size'])
print(data['stargazers_count'])
print(data['forks_count'])
print(data['watchers_count'])

We have completed writing our application. Now let’s see how the final script looks.

Final Script

The final script will look at something, as shown below. We have also added two input statements to let users enter the username and repository name of the repository whose information we want.

# importing the required modules
import json
import requests

# prompting the user to input the required data
username = input("Please Enter The Username: ")
repo = input("Please Enter The Repository Name: ")

# building the url for API
url = f'https://api.github.com/repos/{username}/{repo}'

# sending an HTTP request using request module
r = requests.get(url)

# parsing the json and converting it into python dictionary
data = json.loads(r.text)

# displaying the repository data
print("\n")
print('Repository Name:',data['name'])
print('Repository Description:',data['description'])
print("Programming Language Used:", data['language'])
print("Repository Created:", data['created_at'])
print("Repository Size:", data['size']/1024, "Mb")
print("Total stars:", data['stargazers_count'])
print("Total forks:", data['forks_count'])
print("Total watchers:", data['watchers_count'])
print("\n")
# End of the program

On running the above code, we will get the following output.

final output of the program

the final output of the program

Conclusion

In this tutorial, we have learned how to access and use web APIs like Github API using python’s JSON and requests library. If you have difficulty working with JSON, you can refer to our tutorial on JSON in python.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More