I am building a GAE web app that receives a JSON file through a HTTP POST and persist the data into the application's datastore.
I was able to parse the incoming JSON file to my NDB Model class with the following code:
jsonContent = json.loads(self.request.body)
myTransaction = Transaction()
myTransaction.populate(**jsonContent)
myTransaction.put()
This is the content of the JSON file:
{"name": "Fabio", "message": "Hello World!"}
The challenge I am facing is that this JSON should also include an ID value to be stored in the "Key Name" column of a datastore entity. I tried adding "key_name":"1" or "id":"1" to the JSON file, but the populate() method does not understand them as valid attributes. Instead, when first instantiating the myTransaction object, I had to traditionally set the constructor to Transaction(id="1"). However, the content for attribute ID is stored in the JSON file along with the rest of the data. It doesn't make any sense to actually programatically open the JSON to retrieve each ID and set the constructor before calling populate(). In fact, this does not even work because populate() would still try to parse the attribute ID within the JSON file.
Does anyone know how to parse a JSON file that includes the "Key Name" attribute to a NDB Model class and persist it do the application's datastore?