REST API + Python: How do I read the data the python code pulls from ServiceNow?

amn
Mega Contributor

I need to pull data from ServiceNow with Python and write it into an external database. The data is returned in this format:

{'result': [{'sys_id': '1c741bd70b2322007518478d83673af3', 'number': 'INC0000060', 'short_description': 'Unable to connect to email', 'assignment_group': {'link': 'https://dev12604.service-now.com/api/now/table/sys_user_group/287ebd7da9fe198100f92cc8d1d2154e', 'value': '287ebd7da9fe198100f92cc8d1d2154e'}}]}

How can I get that data into an array of objects that can be looped over for processing?

1 ACCEPTED SOLUTION

amn
Mega Contributor

figured it out. I'm not familiar with python so this took longer than it should have...

print(data['result'][0])

View solution in original post

6 REPLIES 6

amn
Mega Contributor

figured it out. I'm not familiar with python so this took longer than it should have...

print(data['result'][0])

GLewis5
Tera Guru
  1. If you are using the Requests Python library (https://docs.python-requests.org/en/latest/user/quickstart/), please note that it has a build-in JSON parser
  2. If you are trying to pull data ServiceNow and write it into an external database, please note that there is an open-source application that does that very thing here: https://github.com/gflewis/sndml3

Dan H
Tera Guru

print(data['result][0]) should work

 Please mark my answer as helpful/correct if it has helped

Hitoshi Ozawa
Giga Sage
Giga Sage

Something like the following. It's better to use .get() to avoid exception in case the key doesn't exists in the dictionary.

def test01():
    data = {'result': [{'sys_id': '1c741bd70b2322007518478d83673af3', 'number': 'INC0000060', 'short_description': 'Unable to connect to email', 'assignment_group': {'link': 'https://dev12604.service-now.com/api/now/table/sys_user_group/287ebd7da9fe198100f92cc8d1d2154e', 'value': '287ebd7da9fe198100f92cc8d1d2154e'}}]}
    data_list = data.get('result')
    for row in data_list:
        print(f'sys_id:{row.get("sys_id")}, number:{row.get("number")}, short_description:{row.get("short_description")}')


if __name__ == '__main__':
    test01()

Execution result:

sys_id:1c741bd70b2322007518478d83673af3, number:INC0000060, short_description:Unable to connect to email