Python Forum

Full Version: Issues parsing the response from a request
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm making a request call and getting back a response. The response is giving me a problem and I can't seem to parse the results.

new_response = requests.post(scribe_map_color_count, json={"buttonColor":buttonColor})
json_data = new_response.json()
print(json_data)
The print output from this code looks like this...
{u'data': [{u'count': 43.0, u'buttonColor': u'Yellow'}]}

I understand that the u is not an issue but I can't seem to parse this information out. I need to get the count information.
>>> my_json = {u'data': [{u'count': 43.0, u'buttonColor': u'Yellow'}]}
>>> my_json['data'][0]['count']
43.0
>>> 
note that if there are more than one item in the data list you will need to iterate over them
(May-11-2019, 06:05 AM)buran Wrote: [ -> ]
>>> my_json = {u'data': [{u'count': 43.0, u'buttonColor': u'Yellow'}]}
>>> my_json['data'][0]['count']
43.0
>>> 
note that if there are more than one item in the data list you will need to iterate over them

I seriously can not believe I missed this :-( Thank you!
(May-11-2019, 02:46 AM)garnold Wrote: [ -> ]I understand that the u is not an issue but I can't seem to parse this information out.
You get u'something' because you use Python 2,just stop using Python 2.
Python 3 has a new Unicode system,that's much improved over Python 2.
>>> import requests
>>> 
>>> buttonColor = 'Yellow'
>>> r = requests.post('http://httpbin.org/post', json={"buttonColor":buttonColor})
>>> r.status_code
200
>>> json_data = r.json()
>>> print(json_data['json'])
{'buttonColor': 'Yellow'}
>>> print(json_data['json']['buttonColor'])
Yellow