Select specific values from JSON output

Question:

I am querying a REST API and I need to select 2 fields from the adapter output below.
I basically need to make variables from OUT_Detailed Description and OUT_Vendor Ticket Number:

Code:

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)
json_format = json.loads(response)
Description = (json_format['OUT_Detailed Decription'])
Ref_Number = (json_format['OUT_Vendor Ticket Number'])

response.text printed Output:

[{"name":"OUT_HPD_CI","value":"001"},{"name":"OUT_Detailed Description","value":"Student needs a new card issued"},{"name":"OUT_Vendor Ticket Number","value":"INC0000019"}]

Error:

 in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not Response
PS C:Program FilesSB_PythonAD Extract> 

I tried several methods to get just the OUT_Detailed Description and OUT_Vendor Ticket Number values from the output but its all failing.

Asked By: Dinerz

||

Answers:

Have you tried doingjson_format = json.loads(response.content.decode('utf-8')) to translate your response into a string?

Answered By: kwcsfoy

There are 3 Dictionaries in a list, first get dictionary which you want in the list, and get value of "value" key.

Description = (json_format[1]['value')
Ref_Number = (json_format[2]['value'])

But if you want better thing, use this:

def restArrayToDict(restArray):
    dictResult = {}
    for dict in restArray:
        dictResult[dict['name']] = dict['value']
    return dictResult
Answered By: 0x01010