Python Forum

Full Version: How to process JSON response from requests?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
        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
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)
Error:
Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> data = json.loads(string) File "C:\Program Files\Python38\lib\json\__init__.py", line 357, in loads return _default_decoder.decode(s) File "C:\Program Files\Python38\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Program Files\Python38\lib\json\decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 170 (char 169)
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
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'])
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.
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'
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?!