I am attempting to work with an API that requires me to structure the request body in a predefined structure. It's full of multi layered hierarchy and I'm unsure on what's the best way to go about creating such a data structure.
The code below works, but it doesn't look conventially tidy. What is the general consensus on how to go about this?
For reference, I am making a call to the following API: https://cloud.google.com/datastore/docs/reference/data/rest/v1/projects/commit
var transaction = {
transaction: transactionId,
mode: 'TRANSACTIONAL',
mutations: []
};
var entity = {
key: {
kind: kind
},
properties: {}
};
if (args.entityId) {
entity.key.id = args.entityId;
}
for (var property in args) {
if (args[property].dataType) {
var propertyType = args[property].dataType + 'Value';
entity.properties[property] = {};
entity.properties[property][propertyType] = args[property].value;
}
}
var mutation = new Object();
mutation[operationType] = entity;
transaction.mutations.push(mutation)
If I remove entity.properties[property] = {}
my code errors, but that line does just not sit well with me.
Oh, and it is important to mention that I am confined to JavaScript 1.6 since I am working with Google Apps Script.
Your time in answering this is greatly appreciated.