How to retrieve Jira Cloud issues using JQL and Python?

JQL stands for Jira Query Language and is a powerful tool to extract values. While working on the Jira interface, it is easy to leverage JQL. What if we need to retrieve the issues using JQL BUT through Python? This can be done through Jira Cloud REST APIs.

Let us look at the script that helps us enable this task.

Python + Jira Cloud JQL + REST APIs

Pre-requisites:

Jira Cloud REST API:

The latest API version for retrieving Jira issues is v3.0. Let us assume we need to retrieve the issues for a project called ‘PROJECTA’ and the name of the Jira Cloud instance is ‘myjirainstance.atlassian.net’. We will leverage the following API call-

https://myjirainstance.atlassian.net/rest/api/3/search?jql=project=PROJECTA

Step 1: Type in the script below in Eclipse IDE that leverages the above API-

'''
Jira Cloud REST API with JQLs
Please do replace the following values in the script before running the script-
a. username = replace with your username as authorized in Jira Cloud
b. AAAAAAAAAAAAAAAAAAA = replace with the token value from Jira Cloud
c. mycompany.com = replace with your org domain value
d. myjirainstance = replace this with your company’s Jira URL
'''

import base64
import requests

'create a variable called auth to authorize user access with the token value, that is retrieved from Jira Cloud interface'
auth =  "Basic " + base64.b64encode(b'username@mycompany.com:AAAAAAAAAAAAAAAAAAA').decode("utf-8")
'create another variable called headers that concatenates the verification values'
headers = {
   "Accept": "application/json",
   "Content-Type": "application/json",
   "Authorization": auth
}
'create another variable called url to build the URL stating the API and the JQL'
url=https://myjirainstance.atlassian.net/rest/api/3/search?jql=project=PROJECTA
'call the API using the GET method'
get_content=requests.get(url, headers=headers, verify=False)
'display the issues in JSON format'
print(get_content.json())

Step 2: Save this script and run to view the results.

By default, the response object limits itself to just few records. To maximize the records, like you want to retrieve 2000 records; then append the API with the ‘maxResults’ value as shown below-

https://myjirainstance.atlassian.net/rest/api/3/search?jql=project=PROJECTA&maxResults=2000

Once we have the data in JSON format, we can analyze the data and perform further operations. This is so easy and effective way.

Is there an alternate method you have tried? Please do share !!