Python Forum
Issues parsing the response from a request
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues parsing the response from a request
#1
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.
Reply
#2
>>> 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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!
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  request.get to read large response pythonlearner1 7 3,177 Apr-05-2022, 08:21 PM
Last Post: pythonlearner1
  how can I correct the Bad Request error on my curl request tomtom 8 5,070 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Unable to convert request response. johnboy1974 4 3,015 Apr-05-2021, 10:10 AM
Last Post: snippsat
  Empty response to request causing .json() to error t4keheart 1 10,061 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,933 Jun-18-2020, 08:07 AM
Last Post: buran
  Parsing Soap XML response grootkarzijn 7 15,565 Jun-14-2019, 01:30 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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