Search code examples
jsonpython-3.xdictionarykeyhttpresponse

Trying to get values form a list inside a JSON respons


My goal is to loop over a JSON response, grab two values, and build out an API call to load information into a POST to create a policy I am building.

To start out on this, I am trying to get two values from a JSON response to assign as variables to build the POST call, which will be the second step to this. As each different "id" and "name" key is assigned, I would like to build out a JSON payload and send the POST calls one at a time. The keys "id" and "name" occurs multiple times in the response payload and I am having issues capturing the two keys.

JSON response

data =  {
 "data":[
 {
    "id":"02caf2be-3245-4d3d",
    "name":"ORA-FIN-ACTG",
    "description":"Oracle",
    "links":{
       "web":"https://com/",
       "api":"https://com/"
    }
 },
 {
    "id":"03af2f46-fad6-41a1",
    "name":"NBCMAINFRAME",
    "description":"Network",
    "links":{
       "web":"https://com/",
       "api":"https://com/"
    }
 },
 {
    "id":"0649628b-0e3b-48df",
    "name":"CAMS",
    "description":"Customer",
    "links":{
       "web":"https://com/",
       "api":"https://com/"
    }
 },
 {
    "id":"069d4bcf-3e50-4105",
    "name":"SHAREPOINTSITES",
    "description":"Sharepoint",
    "links":{
       "web":"https://com/",
       "api":"https://com/"
    }
 }
],
"took":0.013,
"requestId":"1f364470"

}

I have tried various "for loops" to grab the data. Here is one of the loops below:

data = json.loads(data)
data[0]['data'][0]['name']
for item in range(len(data)):
print(data[item]['data'][0]['name'])

I have also tried reading it as a dictionary:

for data_dict in data:
for key, value in data_dict.items():
    team.append(key)
    id.append(value)

    print('name = ', team)
    print('id = ', id)

I am also getting KeyError's and TypeError: the JSON object must be str, bytes or bytearray, not 'dict'.

Any help is appreciated.

FYI, this is the payload I am wanting to populate with the "name" and "id" values:

data= {
    "type":"alert",
    "description":"policy",
    "enabled":"true",
    "filter":{
        "type":"match-any-condition",
        "conditions":[
            {
                "field":"extra-properties",
                "key":"alertOwner",
                "operation":"equals",
                "expectedValue":name
            }
        ]
    },
    "ignoreOriginalResponders": "true",
    "ignoreOriginalTags": "true",
    "continue": "true",
    "name": str(name) + " Policy",
    "message":"{{message}}",
    "responders":[{"type":"team","id":id}],
    "alias":"{{alias}}",
    "tags":["{{tags}}"],
    "alertDescription":"{{description}}"
    }

Solution

  • The JSON response which you have given is already a dictionary so no need to use json.loads for that. The multiple item list is actually nested under the data key. So you could just simply iterate through the array of items like this:

    for item in data["data"]:
        print("{} : {}".format(item["id"],item["name"]))
    
    

    This is the output:

    02caf2be-3245-4d3d : ORA-FIN-ACTG
    03af2f46-fad6-41a1 : NBCMAINFRAME
    0649628b-0e3b-48df : CAMS
    069d4bcf-3e50-4105 : SHAREPOINTSITE