Selecting values from a JSON file in Python
Question: 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>


Answer: 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)
    

