Reputation: 1657
I have written the following code to read sample JSON data from url,
HEADER = {"content-type": "application/josn"}
session = requests.Session()
session.verify = True
session.headers = HEADER
output = session.request("GET", "https://jsonplaceholder.typicode.com/todos/1", timeout=30)
If I print output
I get,
<Response [200]>
If I do,
output = session.request("GET", "https://jsonplaceholder.typicode.com/todos/1", timeout=30).json()
I get actual json
content,
{u'completed': False, u'userId': 1, u'id': 1, u'title': u'delectus aut autem'}
But when I do,
output = session.request("GET", "https://jsonplaceholder.typicode.com/todos/1", timeout=30)
print(json.loads(output))
I get,
File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
Can someone please give an example to me when should I use .json()
and when json.loads()
?
Upvotes: 0
Views: 772
Reputation: 4213
The .json
is just a shortcut of json.loads()
when the response is a json.
print(json.loads(output))
is not working because you need to get the body of the request, i think it is
print(json.loads(output.text))
Upvotes: 6