Alex Rivera | Logout

Generating json in python for app engine

Asked 2011-05-03T15:48:39.667
10

I am somewhat new to python and I am wondering what the best way is to generate json in a loop. I could just mash a bunch of strings together in the loop, but I'm sure there is a better way. Here's some more specifics. I am using app engine in python to create a service that returns json as a response.

So as an example, let's say someone requests a list of user records from the service. After the service queries for the records, it needs to return json for each record it found. Maybe something like this:

{records: 
{record: { name:bob, email:blah@blah.com, age:25 } },
{record: { name:steve, email:blah@blahblah.com, age:30 } }, 
{record: { name:jimmy, email:blah@b.com, age:31 } }, 
}

Excuse my poorly formatted json. Thanks for your help.

Edit
Report

1 Answer

7

My question is how do I add to the dictionary dynamically? So foreach record in my list of records, add a record to the dictionary.

You may be looking to create a list of dictionaries.

records = []
record1 = {"name":"Bob", "email":"bob@email.com"}
records.append(record1)    
record2 = {"name":"Bob2", "email":"bob2@email.com"}
records.append(record2)

Then in app engine, use the code above to export records as json.

answered 2011-05-03T16:17:40.203

Your Answer