Insert API response into Oracle table using Python

  api, oracle, python

I am trying working with microsoft Graph API, where my goal is to find out all the owners of a group in O365. For this I am using two APIs.
1)https://graph.microsoft.com/v1.0/groups/ -> to find out all the GroupIDs
2)https://graph.microsoft.com/v1.0/groups/{GroupID}/owners/ -> to find all the owners of that group.

I am usiing the below code to call both the APIs and finally inserting the data into Oracle DB. But I am getting KeyError.

url = "https://graph.microsoft.com/v1.0/groups/"
Group_owner_url = "https://graph.microsoft.com/v1.0/groups/{}/owners/"
rows =[]
sysdate = datetime.datetime.now()

response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()   
for group in data['value']:
    GroupID = group['id']
    rows.append(GroupID) 
    
for GroupID in rows:            
    url_owner = Group_owner_url.format(GroupID)
    response_owner = requests.request("GET", url_owner, headers=headers, data=payload)
    owner_data = response_owner.json() 
    for owner in owner_data['value']:
        cursor.execute(
        "insert into <tabel> (GROUP_ID, OWNER_NAME, OWNER_GIVENNAME, OWNER_SURNAME, JOB_TITLE, USER_PRINCIPAL_NAME, EMAIL, MOBILE_PHONE, OFFICE_LOCATION, DB_CREATE_TIME)values (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10)",(GroupID, owner['displayName'],owner['givenName'], owner['surname'], owner['jobTitle'], owner['userPrincipalName'], owner['mail'], owner['mobilePhone'], owner['officeLocation'], sysdate)) 
cursor.close()
connect.commit()
connect.close()

But i am getting this error:
"for owner in owner_data[‘value’]:
KeyError: ‘value’
"
The same code I tried to write into csv file, it worked. But whenever I tried to insert into DB, getting this error.
Where I am making the mistake?

Source: Python Questions

LEAVE A COMMENT