Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<class 'requests.models.Response'> to Json

I've never done any object oriented programming, only basic script writing.

I'm playing around with grequests

rs = (grequests.get('https://api.github.com/repositories?since='+str(page), auth=(login, password)) for page in pages)
blah = grequests.map(rs)
print type(blah[0])

The response is:

<class 'requests.models.Response'>

Normally I convert the response to text and then load it into json so I can parse it, but I can't do that with this response.

I understand the concept of classes but haven't used them or know really what to do with that response.

Is there a way I can convert it to json?

like image 900
Morgan Allen Avatar asked Jul 29 '14 13:07

Morgan Allen


2 Answers

blah[0] in your case is a requests.models.Response class which, according to the source code and the documentation, has json() method that deserializes the JSON response into a Python object using json.loads():

print blah[0].json()
like image 110
alecxe Avatar answered Oct 18 '22 03:10

alecxe


Response object can be converted to JSON in two ways.

  1. Use the method .json()
    blah[0].json()

OR

  1. Convert to text and load as json.
    json.loads(blah[0].text)
like image 3
Krishna Chaitanya Gopaluni Avatar answered Oct 18 '22 04:10

Krishna Chaitanya Gopaluni