I am querying a REST API and I need to select 2 fields from the adapter output below, the query will include multiple incident numbers.
I basically need a list of Incident Numbers and Descriptions from the output below.
Code to get the data:
headers = {'content-type': 'application/json', 'Authentication-Token': authToken}
response = requests.post('http://dev.startschools.local:2031/baocdp/rest/process/:ITSM_Interface:IncidentManagement:QueryIncident/execute', headers=headers, data=json.dumps(get_query_inc_json()))
print(response.text)
response.text printed Output:
[{"name":"Adapter_Output","value":"<query-action-result><entries><metadata><entry-count>2</entry-count></metadata><entry id="INC000003197686|INC000003197686"><field name="Incident Number">INC000021</field><field name="Description">Student needs a new card issued</field></entry><entry id="INC000003198967|INC000003198967"><field name="Incident Number">INC000035</field><field name="Description">Students cannot access the text book portal</field></entry></entries></query-action-result>"}]
I need to get a list of Incident numbers and descriptions so I can loop through them:
List 1 of incident numbers:
["INC000021", "INC000035"]
List 2 of Descriptions:
["Student needs a new card issued", "Students cannot access the text book portal"]
I am trying to use slice to get all the incident numbers into a list but because the output keeps changing the slice is not working.
Anyone know of a more efficient way other than slice to extract the info I need from this output?
2
Answers
Try this.
Even though @jjislam’s answer gives you the right answer for your example, I would recommend you to make use of a proper xml parser to navigate through your result, because this is the actual intent of having structured data in the first place!
This is a snippet on how to use the built-in
xml.etree
package. Once you get what is going on here, you can easily convert it into a single-liner with comprehensions.