hanjaelee
hanjaelee

Reputation: 157

Python, I want to make list json.dumped But, error 'dict' object has no attribute 'dumps'

below is my python code

r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY)         
json = r.json()                                                                                                                                           
items = json.get("items")                                                                                                                                 
videos = []                                                                                                                                               
for x in items:                                                                                                                                           
  title = x["snippet"]["title"]                                                                                                                           
  videoId = x["id"]["videoId"]                                                                                                                            
  channelTitle = x["snippet"]["channelTitle"]                                                                                                             
  cam_thumbnails = x["snippet"]["thumbnails"]["medium"]["url"]                                                                                            
  publishedAt = x["snippet"]["publishedAt"]                                                                                                               
  data = { "title" : title,                                                                                                                               
           "videoId" : videoId,                                                                                                                           
           "channelTitle" : channelTitle,                                                                                                                 
           "cam_thumbnails" : cam_thumbnails,                                                                                                             
           "publishedAt" : publishedAt,                                                                                                                   
           }                                                                                                                                              
  videos.append(data)                                                                                                                                     
print json.dumps(videos) # this code cause problem

I inserted 'dict' to 'list' and then called json.dumps() But, error was arised error messege is 'dict' object has no attribute 'dumps'

What is problem?, and How can I solve this problem?

Upvotes: 11

Views: 26664

Answers (1)

Spirine
Spirine

Reputation: 1877

Previously, you must have imported the json module, writing import json, which creates a variable in your namespace named json.

Then, you do json = r.json(), ie. you assign a new reference to the name json, which doesn't represents the module json anymore, but instead the result of the r.json() method.

Thus, you can't use anymore the json module using the syntax json.function(), because json is the result of r.json().

To resolve your problem, you must change the name of json eg. to json_dict or anything else.

Upvotes: 36

Related Questions