I recently switched from ext.db to the new NDB and having difficulties.
I want to convert structured list into Json package so I can send to an iPhone app. I get "not JSON serializable" error. I want to package so that all the user's favorite fruits are converted to Json. If a user likes apple, orange, and strawberry, then in the favorites
field of Json (below code) should have all three fruits and associated scores and comments.
I know to_dict exist, as well as jsonProperty, but I do not know how to apply if applicable.
Following is what I have:
class FavFruits(ndb.Model):
fruit = ndb.StringProperty()
score = ndb.IntegerProperty()
comment = ndb.TextProperty()
class UserProfile(ndb.Model):
uid = ndb.StringProperty(required=True)
favFruits = ndb.StructuredProperty(FavFruits, repeated=True)
@classmethod
def makeJsonPackage(cls, uid):
fruitList = UserProfile.query(UserProfile.uid == uid).get()
entry = {}
entry["uid"] = fruitList.uid
entry["favorites"] = fruitList.favFruits
return (entry)
# down stream of the code
jsonData = UserProfile.makeJsonPackage(uid)
self.response.write(json.dumps(jsonData))
This does not work.. problem is at entry["favorites"] = fruitList.favFruits
as I am having problem converting structured list into Json data.
Goal: Send the entire list of favFruits
entries (multiple fruits). I would like to keep structuredList as I want to query the data when the user requests, say, "apple", so that I can display the fruit (apple) and associated score and comments.
Any help will be greatly appreciated.