1

I am making a Jira Rest API call to retrieve a data set which has the following format:

{
    "expand": "schema,names",
    "startAt": 0,
    "maxResults": 50,
    "total": 15693,
    "issues": [
        {
            "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
            "id": "12345",
            "self": "https://url.com",
            "key": "random text",
            "fields": {
                "customfield_16570": null,
                "customfield_18873": null,
                "customfield_18470": null,
                "customfield_18471": null,
                "customfield_20271": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345"
                },
                "fixVersions": [
                    {
                        "self": "https://url.com",
                        "id": "12345",
                        "description": "",
                        "name": "random text",
                        "archived": false,
                        "released": false
                    }
                ],
                "customfield_18874": null,
                "resolution": null,
                "customfield_13983": "random text",
                "customfield_18875": null,
                "customfield_19277": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345"
                },
                "customfield_15591": null,
                "customfield_19278": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345"
                },
                "lastViewed": null,
                "customfield_17370": null,
                "customfield_15594": null,
                "customfield_19273": null,
                "customfield_19274": null,
                "customfield_17771": null,
                "customfield_19275": null,
                "customfield_18187": null,
                "customfield_19276": null,
                "priority": null,
                "labels": [
                    "random text"
                ],
                "timeestimate": null,
                "aggregatetimeoriginalestimate": 0,
                "customfield_19270": "random text",
                "issuelinks": [],
                "customfield_19271": null,
                "customfield_19272": null,
                "customfield_18183": null,
                "assignee": {
                    "self": "https://url.com",
                    "name": "random text",
                    "key": "random text",
                    "emailAddress": "random text",
                    "avatarUrls": {
                        "48x48": "https://url.com",
                        "24x24": "https://url.com",
                        "16x16": "https://url.com",
                        "32x32": "https://url.com"
                    },
                    "displayName": "random text",
                    "active": true,
                    "timeZone": "random text"
                },
                "status": {
                    "self": "https://url.com",
                    "description": "random text",
                    "iconUrl": "https://url.com",
                    "name": "Open",
                    "id": "1",
                    "statusCategory": {
                        "self": "https://url.com",
                        "id": 2,
                        "key": "new",
                        "colorName": "random text",
                        "name": "random text"
                    }
                },
                "customfield_18970": null,
                "components": [],
                "customfield_12071": "random text",
                "customfield_12070": null,
                "customfield_15583": null,
                "customfield_16672": null,
                "customfield_18570": null,
                "customfield_14371": null,
                "customfield_16671": null,
                "customfield_20370": "",
                "customfield_16670": null,
                "customfield_14771": null,
                "customfield_10970": "12345",
                "aggregatetimeestimate": null,
                "customfield_17076": null,
                "customfield_17075": {
                    "self": "https://url.com",
                    "value": "No",
                    "id": "12345"
                },
                "customfield_17470": null,
                "creator": {
                    "self": "https://url.com",
                    "name": "random text",
                    "key": "random text",
                    "emailAddress": "random text",
                    "avatarUrls": {
                        "48x48": "https://url.com",
                        "24x24": "https://url.com",
                        "16x16": "https://url.com",
                        "32x32": "https://url.com"
                    },
                    "displayName": "random text",
                    "active": true,
                    "timeZone": "random text"
                },
                "subtasks": [],
                "customfield_17077": null,
                "reporter": {
                    "self": "https://url.com",
                    "name": "vavoicu@contractor.ea.com",
                    "key": "vavoicu@contractor.ea.com",
                    "emailAddress": "vavoicu@contractor.ea.com",
                    "avatarUrls": {
                        "48x48": "https://url.com",
                        "24x24": "https://url.com",
                        "16x16": "https://url.com",
                        "32x32": "https://url.com"
                    },
                    "displayName": "random text",
                    "active": true,
                    "timeZone": "random text"
                },
                "customfield_14001": null,
                "aggregateprogress": {
                    "progress": 0,
                    "total": 0
                },
                "customfield_10562": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345"
                },
                "customfield_12976": null,
                "customfield_15604": null,
                "progress": {
                    "progress": 0,
                    "total": 0
                },
                "customfield_19370": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345",
                    "child": {
                        "self": "https://url.com",
                        "value": "random text",
                        "id": "12345"
                    }
                },
                "votes": {
                    "self": "https://url.com",
                    "votes": 0,
                    "hasVoted": false
                },
                "customfield_16372": null,
                "issuetype": {
                    "self": "https://url.com",
                    "id": "1",
                    "description": "random text",
                    "iconUrl": "https://url.com",
                    "name": "random text",
                    "subtask": false,
                    "avatarId": 12345
                },
                "customfield_16371": null,
                "customfield_18674": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345"
                },
                "customfield_16772": null,
                "customfield_16771": null,
                "timespent": null,
                "customfield_16373": null,
                "customfield_20470": null,
                "project": {
                    "self": "https://url.com",
                    "id": "12345",
                    "key": "random text",
                    "name": "random text",
                    "projectTypeKey": "software",
                    "avatarUrls": {
                        "48x48": "https://url.com",
                        "24x24": "https://url.com",
                        "16x16": "https://url.com",
                        "32x32": "https://url.com"
                    },
                    "projectCategory": {
                        "self": "https://url.com",
                        "id": "12345",
                        "description": "",
                        "name": "random text"
                    }
                },
                "aggregatetimespent": null,
                "customfield_14108": [
                    {
                        "self": "https://url.com",
                        "value": "random text",
                        "id": "12345"
                    }
                ],
                "resolutiondate": null,
                "workratio": 0,
                "customfield_19871": null,
                "watches": {
                    "self": "https://url.com",
                    "watchCount": 0,
                    "isWatching": false
                },
                "customfield_17172": null,
                "customfield_17970": null,
                "created": "2021-03-04T06:51:01.000-0800",
                "customfield_17176": null,
                "customfield_19870": null,
                "customfield_17972": null,
                "customfield_15953": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345"
                },
                "customfield_17170": {
                    "self": "https://url.com",
                    "value": "random text",
                    "id": "12345"
                },
                "updated": "2021-03-04T14:49:24.000-0800",
                "customfield_16471": null,
                "customfield_16470": null,
                "customfield_18371": null,
                "customfield_18372": null,
                "customfield_14570": "1|i0jlfo:",
                "timeoriginalestimate": 0,
                "description": "random text",
                "customfield_13000": null,
                "customfield_16872": null,
                "customfield_10643": null,
                "customfield_10644": null,
                "customfield_10888": {
                    "self": "https://url.com",
                    "value": "C - Low",
                    "id": "12345"
                },
                "customfield_18370": null,
                "summary": "Random text",
                "customfield_17670": null,
                "customfield_18881": null,
                "customfield_15371": [
                    {
                        "self": "url.com",
                        "value": "Demo",
                        "id": "12345"
                    }
                ],
                "customfield_18882": null,
                "customfield_18883": null,
                "customfield_19570": {
                    "self": "https://url.com",
                    "id": "12345",
                    "description": "",
                    "name": "Demo",
                    "archived": false,
                    "released": false
                },
                "customfield_15372": null,
                "customfield_11572": null,
                "customfield_13997": "item NR",
                "customfield_18886": null,
                "environment": null,
                "duedate": null,
                "customfield_17270": null
            }
        }    
    ]
}

At first I've tried using a requests.get() syntax in order to get the full list of items but the problem was that my call retrieved only 50 results instead of 15693 as stated in "total" field. So... I am struggling to figure out how can I loop through all those 15693 results and write them into a new JSON file using Python. Below is the code used:

import requests
import json
import logging
import stdiomask
import argparse

logging.captureWarnings(True) # 


# ap = argparse.ArgumentParser()
# ap.add_argument("-n", "--name", required=True, help="Jira user")
# ap.add_argument("-p", "--pass", required=True, help="Jira password")
# args=vars(ap.parse_args())
# userJira = args["name"]
# passJira = args["pass"]


serverURL = 'https://jiraurl.com'
user = input("Jira user: ")
password = stdiomask.getpass(prompt='Jira Password: ', mask='*')
# user = userJira
# password = passJira
jql = '/rest/api/2/search?jql=project = Demo AND issuetype = BUG ORDER BY createdDate DESC'

response = requests.get(serverURL + jql,verify=False,auth=(user, password))

#=================================================================================================
#==== | Save json results | ====
#=================================================================================================
with open('test.json', 'w', encoding='utf-8') as f:
    json.dump(response.json(), f, ensure_ascii=False, indent=4)
#=================================================================================================

So this code gets the job done but only for the first 50 items

Then after reading other posts related to this topic I've tried this:

import json
import stdiomask
import requests

from jira import JIRA

user = input("Jira user: ")
password = stdiomask.getpass(prompt='Jira Password: ', mask='*')
options = {'server': 'https://url.com'}
jira = JIRA(options, basic_auth=(user, password))
size = 200
initial = 0
while True:
    start= initial*size
    issues = jira.search_issues('project = Demo AND issuetype = BUG ORDER BY createdDate DESC',  start,size)
    if len(issues) == 0:
        break
    initial += 1
    for issue in issues:
        print(issue)

With this code I am able to loop through all of the items and print their names in the console, but I need to get the complete data set in json format for each item (not just the ticket name) and save it into a JSON file.

Adi Petrescu
  • 121
  • 1
  • 3
  • 12
  • Presumably it has something to do with "startAt: 0" and "maxResults: 50". – mkrieger1 Mar 05 '21 at 08:12
  • I've mixed up the steps performed so I think the description got confusing, I've edited that so you can get a better understanding. With the python code posted i can get all the issues but I can't figure out how to save them in full format to a new JSON file....that's the issue – Adi Petrescu Mar 05 '21 at 08:22
  • Does this answer your question? [How to access the next page using JIRA -REST-API for python](https://stackoverflow.com/questions/33815378/how-to-access-the-next-page-using-jira-rest-api-for-python) – mkrieger1 Mar 05 '21 at 09:39
  • It's the same concept I'm using at the moment within the posted script: but i need to get the data in json format and save it to a json file. Not sure if this is achievable through the jira module or should i return to requests again – Adi Petrescu Mar 05 '21 at 09:45

0 Answers0