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

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)
dnez
  • 141
  • 1
  • 11
  • Which datastore library are you using? Might be good to show the code as well. Usually the key is an object, which might not serialize properly. Some libraries offer ways of encoding keys as strings. – Dan Cornilescu Aug 30 '17 at 21:51
  • No sql and NDB model – dnez Aug 30 '17 at 22:04

1 Answers1

1

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

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • Thanks! headed to wrong direction previously. I was just looking at the key class documentation: https://cloud.google.com/appengine/docs/standard/python/ndb/keyclass This will return id, id = sandy_key.id() – dnez Aug 31 '17 at 03:30
  • 1
    Side note, if you use the IDs- they do not preserve the ancestry information, so they're only unique within the entities with the same parent key (or no parent key). To re-construct the original entity key you need its parent key. The key urlsafe string contains the entire ancestry. Not an issue if you don't use the entity ancestry. – Dan Cornilescu Aug 31 '17 at 03:39
  • 1
    One thing to note, the url safe key also includes an app id so if you are transferring data to another app, it may not work as desired. – new name Aug 31 '17 at 12:21
  • 1
    The kind is also part of the key, so you can't have an object of a different kind having the same key. But they can have the same ID. – Dan Cornilescu Aug 31 '17 at 19:46