Cannot turn response.text into a dictionary in Python
Question:
I am using the Python request module and having trouble converting my response.text into a Python dictionary.
def my_function():
url = API_URI["test"]
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(api), headers=headers)
print(r.text)
dict = r.json()
return dict
my_function()
Here is the printed response from r.text
{"subscriptionId":"8530989","profile":{"customerProfileId":"507869879","customerPaymentProfileId":"512588514"},"refId":"12345","messages":{"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}}
And the error
raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)
I have tried both json.loads(r.text) and r.json()
I see that the error wants me to decode, but where?
Answers:
As the error points out, the data should be decoded using utf-8-sig
instead of the default utf-8
decoding.
something like this:
decoded_data = r.text.encode().decode('utf-8-sig')
return json.loads(decoded_data)
would probably work to load this data into a dictionary.
Edit:
Setting
r.encoding = 'utf-8-sig'
before running r.json()
should also fix this.
I am using the Python request module and having trouble converting my response.text into a Python dictionary.
def my_function():
url = API_URI["test"]
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(api), headers=headers)
print(r.text)
dict = r.json()
return dict
my_function()
Here is the printed response from r.text
{"subscriptionId":"8530989","profile":{"customerProfileId":"507869879","customerPaymentProfileId":"512588514"},"refId":"12345","messages":{"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}}
And the error
raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)
I have tried both json.loads(r.text) and r.json()
I see that the error wants me to decode, but where?
As the error points out, the data should be decoded using utf-8-sig
instead of the default utf-8
decoding.
something like this:
decoded_data = r.text.encode().decode('utf-8-sig')
return json.loads(decoded_data)
would probably work to load this data into a dictionary.
Edit:
Setting
r.encoding = 'utf-8-sig'
before running r.json()
should also fix this.