5

I am getting JIRA data using the following python code,

how do I store the response for more than one key (my example shows only one KEY but in general I get lot of data) and print only the values corresponding to total,key, customfield_12830, summary

    import requests
    import json
    import logging
    import datetime
    import base64
    import urllib
    serverURL = 'https://jira-stability-tools.company.com/jira'
    user = 'username'
    password = 'password'
    query = 'project = PROJECTNAME AND "Build Info" ~ BUILDNAME  AND assignee=ASSIGNEENAME'
    jql = '/rest/api/2/search?jql=%s' % urllib.quote(query)
    response = requests.get(serverURL + jql,verify=False,auth=(user, password))
    print response.json()

response.json() OUTPUT:-

http://pastebin.com/h8R4QMgB

8
  • If the structure you trimmed here to show only one item differs from the structure you need to support with more than one item, then any answer we give you is liable not to be useful in the other scenario. Commented Jun 10, 2016 at 5:02
  • stackoverflow.com/questions/28069753/… related quetion. check it out too. Commented Jun 10, 2016 at 5:05
  • BTW, in general, if you have deeply nested structure (like this one!) that you want to figure out, pprint is your friend. See docs.python.org/2/library/pprint.html Commented Jun 10, 2016 at 5:05
  • Charles - is there an external link I can paste my sample output for more than item and share here? Commented Jun 10, 2016 at 5:05
  • 1
    @PythonProg, ...you may note that pastebin.com is the one pastebin I asked you not to use -- it's full of ads for anyone not using AdBlock or similar. Commented Jun 10, 2016 at 15:03

1 Answer 1

1

From the the link you pasted to pastebin and from the json that I saw, its a you issues as list containing key, fields(which holds custom fields), self, id, expand.

You can simply iterate through this response and extract values for keys you want. You can go like.

data = response.json()
issues = data.get('issues', list())

x = list()

for issue in issues:
    temp = {
        'key': issue['key'],
        'customfield': issue['fields']['customfield_12830'],
        'total': issue['fields']['progress']['total']
    }
    x.append(temp)
print(x)

x is list of dictionaries containing the data for fields you mentioned. Let me know if I have been unclear somewhere or what I have given is not what you are looking for.

PS: It is always advisable to use dict.get('keyname', None) to get values as you can always put a default value if key is not found. For this solution I didn't do it as I just wanted to provide approach.

Update: In the comments you(OP) mentioned that it gives attributerror.Try this code

data = response.json()
issues = data.get('issues', list())

x = list()

for issue in issues:
    temp = dict()
    key = issue.get('key', None)
    if key:
       temp['key'] = key 

    fields = issue.get('fields', None)
    if fields:
        customfield = fields.get('customfield_12830', None)
        temp['customfield'] = customfield

        progress = fields.get('progress', None)
        if progress:
            total = progress.get('total', None)
            temp['total'] = total

    x.append(temp)
print(x)
Sign up to request clarification or add additional context in comments.

10 Comments

this gives an error AttributeError: 'NoneType' object has no attribute 'get'
am using Python 2.7.3,does it work for this version,it throws as error as I stated above
Thats the reason I mentioned that use, dict.get('keyname', None) to ensure that the key which you are trying to access indeed exists. For you this error is raised when you are trying to access either 'fields' whose value is dictionary again or may be 'progress' which again is dictionary. To avoid it use fields = issue.get('fields', None). Then if fields then try to access your customfield within it
Rajesh - did you use any tools to decipher this structure?what do you suggest for a beginner to understand this structure and had to do it himself?
@PythonProg, ...I also did previously suggest pprint as a tool to decipher the structure once it's in the Python world. On the JSON end, jq or just python -m json.tool will often suffice.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.