1
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
1
  • 2
    mydata["result"] is a dictionary. Iterating over it iterates over the keys, so item is e.g. "sys_id" and "sys_id"["sys_id"] doesn't make sense. Commented Nov 11, 2019 at 22:35

1 Answer 1

1

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"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.