Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 206
hanjaelee Avatar asked May 08 '15 08:05

hanjaelee


People also ask

How do I fix AttributeError dict object has no attribute append?

The Python "AttributeError: 'dict' object has no attribute 'append'" occurs when we try to call the append() method on a dictionary. To solve the error, use bracket notation to add a key-value pair to a dict or make sure to call the append() method on a list.

What is the difference between JSON dump and dumps?

json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.

What is JSON dump?

The json. dumps() method allows us to convert a python object into an equivalent JSON object. Or in other words to send the data from python to json. The json. dump() method allows us to convert a python object into an equivalent JSON object and store the result into a JSON file at the working directory.

What is FP in JSON dump?

A fp is a file pointer used to write JSON formatted data into file. Python json module always produces string objects, not bytes objects, therefore, fp.


1 Answers

Previously, you must have imported the json module, writing import json, which creates a variable in your namespace, whose name is 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 the variable named json in your example, to, for example, json_dict or anything else.

like image 167
Spirine Avatar answered Oct 11 '22 13:10

Spirine