dict object is not callable on json, requests module

So, you are using the amazing requests module of Python! So checked their sample code in the website: http://docs.python-requests.org/en/latest/ and the URL you are calling returns json content. So you followed their example and came up with a code like this:

import requests
url = '' # put your url here
r = requests.get(url)
json_content = r.json()
print json_content

When you run the program, you get the following error:
json_content = r.json()
TypeError: 'dict' object is not callable
Now you are wondering what is this! An example from the home page doesn't work. So you searched Google and came to my website. Now you solve the problem using json_content = r.json instead of json_content = r.json()
So this code with work (Python 2.7):

import requests
url = '' # put your url here
r = requests.get(url)
json_content = r.json
print json_content

If you are wondering why you got the error in the first code, let me give you a hint. Add the following lines to your Python code and see the output:
print type(r)
print type(r.json)

Comments

matthew said…
In my case, I just needed to upgrade requests: pip install requests -U
Anonymous said…
I was seeing this problem from within a library so I didn't really want to change it. Upgrading requests seems to have resolved things though! :)

Thanks
Ray said…
So I met the same problem and google led me to here, but your post is disappointed.

Even you traced down to the culprit, but you failed to provide a meaningful conclusion: which requests version is the latest and what is its r.json behavior.

Let's cut to the chase. "Upgrade requests and it works!" Period.
Tamim Shahriar said…
The conclusion is given as a hint in the last few lines. :)
Jonathan Platt said…
Spent 15 minutes trying to decipher the blog post, and the answer was in @matthew's comment. Thanks!
Unknown said…
>
show this type of error
Nelson Castillo said…
I just upgraded requests. The version in (current) Debian Stable is ancient.

Popular posts from this blog

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code

Adjacency Matrix (Graph) in Python