How to process JSON response from requests? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to process JSON response from requests? (/thread-23631.html) |
How to process JSON response from requests? - Heinrich - Jan-09-2020 try: message_to_clientsservice = { 'id': id_id, 'name': 'create-member-client-for-tenant-setup', 'payload': { 'clientName': client_name, 'kbCorporationId': int(kb_corp_id), 'email': email, 'clientTypeId': client_type_id, 'address': { 'countryCode': country_code, }, 'phoneNumbers': [] } } clientsservice_request_header = { 'Content-Type': 'application/json', 'ServiceAccessToken': service_access_token, 'Ocp-Apim-Subscription-Key': ocp_apim_subscription_key, 'Authorization': 'Bearer ' + bearer_token } clients_service_response = requests.post(clientsservice_url, headers=clientsservice_request_header, json=message_to_clientsservice) print(clients_service_response.status_code) string = clients_service_response.text print(string) data = json.loads(string) print(type(data))Output: 200 {"id":"098039c2-7585-4a84-9259-fef6b4b22278","name":"create-member-client-for-tenant-setup-response","correlationId":"a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1","payload":"{\"newlyCreatedClient\":{\"clientId\":7296,\"clientName\":\"Milk Man 1\",\"corporationId\":4520226,\"corporationName\":\"Milk Man 1\"},\"suggestedExistingClients\":null,\"success\":true,\"errorMessage\":\"\"}"} Something went wrong! Try again or contact your Sysadmin! [09/Jan/2020 15:24:58] "POST /create_member/ HTTP/1.1" 200 7885 It simply skips "data = json.loads(string)" what am I doing wrong? I also tried this without success. Essentially using requests.json method. I also tried the below without success. Using requests.json method. try: message_to_clientsservice = { 'id': id_id, 'name': 'create-member-client-for-tenant-setup', 'payload': { 'clientName': client_name, 'kbCorporationId': int(kb_corp_id), 'email': email, 'clientTypeId': client_type_id, 'address': { 'countryCode': country_code, }, 'phoneNumbers': [] } } clientsservice_request_header = { 'Content-Type': 'application/json', 'ServiceAccessToken': service_access_token, 'Ocp-Apim-Subscription-Key': ocp_apim_subscription_key, 'Authorization': 'Bearer ' + bearer_token } # print(message_to_clientsservice) # print(type(message_to_clientsservice)) # print(json.dumps(message_to_clientsservice)) # print(type(clientsservice_request_header)) # print(clientsservice_request_header) clients_service_response = requests.post(clientsservice_url, headers=clientsservice_request_header, json=message_to_clientsservice) print(clients_service_response.status_code) #string = clients_service_response.text data = clients_service_response.json() print(type(data))Output: 200 Something went wrong! Try again or contact your Sysadmin! [09/Jan/2020 15:19:24] "POST /create_member/ HTTP/1.1" 200 7885 RE: How to process JSON response from requests? - Clunk_Head - Jan-09-2020 You should include the except code that goes with your try code. I took your json and plugged it into my Idle shell. Here's what happened. import json string = '{"id":"098039c2-7585-4a84-9259-fef6b4b22278","name":"create-member-client-for-tenant-setup-response","correlationId":"a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1","payload":"{\"newlyCreatedClient\":{\"clientId\":7296,\"clientName\":\"Milk Man 1\",\"corporationId\":4520226,\"corporationName\":\"Milk Man 1\"},\"suggestedExistingClients\":null,\"success\":true,\"errorMessage\":\"\"}"}' data = json.loads(string) So it's not ignoring it, rather it's throwing an exception and your except block is replacing that with a message:Something went wrong! Try again or contact your Sysadmin! [09/Jan/2020 15:24:58] "POST /create_member/ HTTP/1.1" 200 7885 RE: How to process JSON response from requests? - Heinrich - Jan-09-2020 Thank you for pointing this out! Good catch! That indicates that the JSON which is returned to me by the API is incorrectly formatted? I tried the following. Using simplejson instead of json module. Worked roght out of the box by just import simplejson as json Here the code excerpt: import simplejson as json clients_service_response = requests.post(clientsservice_url, headers=clientsservice_request_header, json=message_to_clientsservice) print(clients_service_response.status_code) print(clients_service_response.encoding) data = json.loads(clients_service_response.text) print(data['id']) RE: How to process JSON response from requests? - Clunk_Head - Jan-09-2020 Excellent, Good Work. When you are using try/except blocks and you run into an issue examine the exception or error more closely. Also, try to only catch specific exceptions. Your response was code 200 so you should not have told the user to check with admin. RE: How to process JSON response from requests? - snippsat - Jan-09-2020 Requests as you use has build in JSON decoder. So then there is no need for separate module like simplejson or json(standard library). So this should work. data = clients_service_response.json() print(data['id'])Demo. >>> import requests >>> >>> my_id = '999' >>> r = requests.post('http://httpbin.org/post', json={"id": my_id}) >>> r.status_code 200 >>> data = r.json() >>> data['json']['id'] '999' RE: How to process JSON response from requests? - Heinrich - Jan-10-2020 That is true. But requests' json method cannot handle it in my case similar to standard lib json. However, simplejson can. Since simplejson is a bit "advanced" eventually at some point json and requests lib will be able to handle in the future?! |