Reputation: 217
import requests
user = 'my_user'
pwd = 'my_pwd'
headers = {"Content-Type":"application/json","Accept":"application/json"}
url = 'https://my_company.service-now.com/api/now/table/sc_req_item/bfa27ef2db203f00d8909c47db9619f3?sysparm_fields=sys_id%2Crequest'
response = requests.get(url, auth=(user, pwd), headers=headers )
mydata = response.json()
print(mydata) #Print for debug output purposes
for item in mydata["result"]:
print(item["sys_id"])
print(item["request"]["value"])
Output from print(mydata)
:
{'result': {'sys_id': 'bfa27ef2db203f00d8909c47db9619f3', 'request': {'link': 'https://my_company.service-now.com/api/now/table/sc_request/3fa27ef2db203f00d8909c47db9619f3', 'value': '3fa27ef2db203f00d8909c47db9619f3'}}}
Error:
Traceback (most recent call last):
File "./prod_getUnapprovedSIDs.py", line 42, in <module>
print(item["sys_id"])
TypeError: string indices must be integers
Upvotes: 1
Views: 107
Reputation: 149
You need to iterate over the key, value
pairs of the JSON response. Try this example instead:
for key, item in mydata.items():
print(item["sys_id"])
print(item["request"]["value"])
Upvotes: 1