How to print a variable with Requests and JSON
Two things, first, make sure you are using the latest version of requests
(its 1.1.0); in previous versions json
is not a method but a property.
>>> r = requests.get('https://api.github.com/users/burhankhalid')>>> r.json['name']u'Burhan Khalid'>>> requests.__version__'0.12.1'
In the latest version:
>>> import requests>>> requests.__version__'1.1.0'>>> r = requests.get('https://api.github.com/users/burhankhalid')>>> r.json()['name']u'Burhan Khalid'>>> r.json<bound method Response.json of <Response [200]>>
But, the error you are getting is because your URL isn't returning valid json, and you are trying to call on None
, what is returned by the property:
>>> r = requests.get('http://www.google.com/')>>> r.json # Note, this returns None>>> r.json()Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'NoneType' object is not callable
In conclusion:
- Upgrade your version of
requests
(pip install -U requests
) - Make sure your URL returns valid JSON
Firstly is myData actually returning anything?
If it is then you can try the following rather than work with the .json() function
Import the Json package and use the Json loads function on the text.
import jsonnewdata = json.loads(myData.text())