Search code examples
pythongoogle-app-enginekeyentitydatastore

How to loop the following datastore data with python and ndb model and return to json.dumps?


[User(key=Key('User', 5275456790069248), auth_ids=[u'[email protected]'], created=datetime.datetime(2017, 8, 29, 22, 50, 36, 297407), email='[email protected]'), 
User(key=Key('User', 5838406743490560), auth_ids=[u'[email protected]'], created=datetime.datetime(2017, 8, 29, 16, 23, 16, 406468), email='[email protected]'), 
User(key=Key('User', 6401356696911872), auth_ids=[u'[email protected]'], created=datetime.datetime(2017, 8, 30, 12, 34, 51, 816926), email='[email protected]')]

I managed to query the above data from Google App Engine datastore using Python but I am unable to loop it into JSON. Don't quite understand the concept of the Keys even I have gone through the documentations.

Besides hoping to understand the way to dump the entire object with the key into JSON without limits, any idea how could I just to extract the 'key'(in string), 'email' and 'created' (DateTime)? By the way, I am trying to retrieve all data.

For example:

users_new_list = []

allusers = User.query().fetch()
for user in all_users:
    #How to get key in string and pass into 'users_new_list'
   users_new_list.append(keyString)

Solution

  • The Key in NDB is a python class instance (an object) which doesn't serialize properly in json. But it comes with the .urlsafe() method converting it to a string which can be json-dumped. From Retrieving Entities from Keys:

    You can also use an entity's key to obtain an encoded string suitable for embedding in a URL:

    url_string = sandy_key.urlsafe()
    

    This produces a result like agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM which can later be used to reconstruct the key and retrieve the original entity:

    sandy_key = ndb.Key(urlsafe=url_string)
    

    Now, to json-encode the entire entity you'd have to write a custom method which would convert the Key properties to strings first. See How to make a class JSON serializable