Ben
Ben

Reputation: 16524

retrieve json from google app engine datastore

I have stored some simple data in the GAE datastore. Now I'd like to pull it out, but I'd like to do so with the results as JSON. Is there a simple way to do this?

Upvotes: 3

Views: 2233

Answers (2)

Yasser
Yasser

Reputation: 1808

You can first convert your datastore model to a dictionary and then use simplejson (python 2.5) or json (python 2.7) package to convert the dictionary to json. Typically this is how the last line of your handler will look like:

self.response.out.write(simplejson.dumps(some_datastore_entity.to_dict()))

The new ndb interface to the datastore provides a to_dict method by default. You can check it out here

Upvotes: 5

Takahiro
Takahiro

Reputation: 1262

class Handler(webapp2.RequestHandler):
    def get(self):
        <do your GQLQuery here>
        self.response.headers['Content-Type'] = 'application/json'
        self.response.body = json.dumps(<your data in dict or dict list>)
        self.response.set_status(200)

Upvotes: 1

Related Questions