Question about json.loads()

Hi,

I am trying to get the content to print as a python list. But it says json.loads(page) needs to be a string? When I use pythonlist = page.json() it works. Why is that? Should I never use json.loads() and always use .json()?

page = requests.get(“https://api.datamuse.com/words?ml=ringing+in+the+ears”)

pythonlist = json.loads(page)
print(pythonlist)

What is the type of page? E.g. type(page).

1 Like

json() method of response, that is shipped with Requests library, does loading for you. If you don’t want to use this shortcut, you can do:

pythonlist = json.loads(page.content)

page is a response object that has other attributes than sole content.

1 Like

print(type(page))

output:

<class ‘requests.models.Response’>

Got you. That makes sense. Also I believe I can do:

pythonlist = json.loads(page.text)

Instead of page.content, page.text also works. Is there a reason I should use the one over the other?

No, actually page.text is better, as it does decoding for you: https://2.python-requests.org/en/master/user/quickstart/#response-content.