Python Forum
How to process JSON response from requests?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to process JSON response from requests?
#1
        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
Reply
#2
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
Reply
#3
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'])
Reply
#4
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.
Reply
#5
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'
Reply
#6
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?!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,369 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Name not found in response json NewbiePyt 4 1,065 Dec-29-2022, 11:12 AM
Last Post: buran
  python requests library .JSON() error mHosseinDS86 6 3,407 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Python3 requests.models.Response ogautier 4 5,363 Feb-17-2022, 04:46 PM
Last Post: ogautier
  JSON response from REST service get nested value nl2ttl 2 2,523 Nov-30-2020, 09:34 PM
Last Post: nl2ttl
  parser json response absolut 4 2,818 Sep-15-2020, 12:10 PM
Last Post: buran
  Python Requests package: Handling xml response soumyarani 1 2,154 Sep-14-2020, 11:41 AM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 10,045 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  API JSON response missing list gives keyerror rolfmadsen 3 3,454 Mar-28-2020, 10:12 AM
Last Post: buran
  Split of key from JSON response aswini_dubey 1 2,404 Dec-04-2019, 06:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020