Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: the JSON object must be str, not 'Response' with Python 3.4

I'm getting this error and I can't figure out what the problem is:

Traceback (most recent call last):
  File "C:/Python34/Scripts/ddg.py", line 8, in <module>
    data = json.loads(r)
  File "C:\Python34\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'Response'

This is my code:

import requests, json

search_q = input('Enter query: ')

r = requests.get('https://api.duckduckgo.com/?q=%s&format=json&pretty=1' % search_q)

if r.status_code == requests.codes.ok:
    data = json.loads(r)
    node = data['RelatedTopics']['Result']
    print (str(node))
else:
    bad_r.raise_for_status()
like image 316
Y7da Avatar asked Jun 04 '26 19:06

Y7da


1 Answers

No need to load the JSON manually; the response has a json method.

data = r.json()
like image 178
Daniel Roseman Avatar answered Jun 06 '26 09:06

Daniel Roseman