Slice and extract specific values from JSON output
Question:
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?
Answers:
Try this.
data = response.text
value = data[0]["value"]
i_nums = [x.split("</field")[0] for x in value.split('<field name="Incident Number">')[1:]]
desc = [x.split("</field")[0] for x in value.split('<field name="Description">')[1:]]
print(i_nums)
print(desc)
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.
import xml.etree.ElementTree
responseText = [{"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>"}]
# the root will be the topmost element, in this case the one with tag "query-action-result"
root = xml.etree.ElementTree.fromstring(responseText[0]["value"])
incidents, descriptions = [], []
# iterate recursively over all elements with tag "entry" that are children of the root element
for entry in root.iter('entry'):
# within each entry, find the first element with tag "field" and attribute "name=Incident Number", and capture its content
incident = entry.find("field[@name='Incident Number']").text
# same, but with attribute "name=Description"
description = entry.find("field[@name='Description']").text
print(incident, description)
incidents.append(incident)
descriptions.append(description)
print(incidents)
print(descriptions)
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?
Try this.
data = response.text
value = data[0]["value"]
i_nums = [x.split("</field")[0] for x in value.split('<field name="Incident Number">')[1:]]
desc = [x.split("</field")[0] for x in value.split('<field name="Description">')[1:]]
print(i_nums)
print(desc)
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.
import xml.etree.ElementTree
responseText = [{"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>"}]
# the root will be the topmost element, in this case the one with tag "query-action-result"
root = xml.etree.ElementTree.fromstring(responseText[0]["value"])
incidents, descriptions = [], []
# iterate recursively over all elements with tag "entry" that are children of the root element
for entry in root.iter('entry'):
# within each entry, find the first element with tag "field" and attribute "name=Incident Number", and capture its content
incident = entry.find("field[@name='Incident Number']").text
# same, but with attribute "name=Description"
description = entry.find("field[@name='Description']").text
print(incident, description)
incidents.append(incident)
descriptions.append(description)
print(incidents)
print(descriptions)