views:

35

answers:

1

Hello,

I have the following code that I need help encoding to JSON and appending the data to a file, foo.json

Here is my code:

user = request.user
print 'USER ID ', user.id // 83
sour = json.dumps({"toast" : [{"uid" : user.id }]})
print 'Sour Toast: ', sour.toast # I'm getting this error: AttributeError: 'str' object has no attribute 'toast'

Basically I want to create a json file which contains the user.id value that can accessible from my front end through jQuery.

If anyone can help me with the error I'm getting or with any tips on where to go after i fix this error, I would greatly appreciate it.

+3  A: 

json.dumps returns you a string. So when you do:

sour = json.dumps({"toast" : [{"uid" : user.id }]})
print sour

Simple prints a string that looks like this:

{"toast" : [{"uid" : user.id }]}

Not an actual object or dict you can get individual values from, just a string you can print or write to a file or whatever else you want to do with strings. It looks like you want to print this:

Source Toast: [{"uid":83}]

To do that you'd want to do the following:

sour = json.dumps([{"uid":83}])
print "Sour Toast: ", sour
mikeocool