11

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?

3
  • 5
    Your variable is called json, as is the module json, don't call your variable json and the problem will be gone Commented May 8, 2015 at 8:19
  • if you have imported json module, since you're assigning a shadow name of json with result r.json(), json has become a dictionary. change json to other name and ensure you import json module, should work Commented May 8, 2015 at 8:21
  • @Anzel ok, Thank you, I solved this problem, good luck! Commented May 8, 2015 at 8:25

1 Answer 1

36

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.