Save JSON data as columns in Azure Table Storage
How do I format my json data and / or modify my function to be stored as columns in Azure table storage?
I am sending json string to IoT hub:
{"ts":"2017-03-31T02:14:36.426Z","timeToConnect":"78","batLevel":"83.52","vbat":"3.94"}
I am running a sample function (in the Azure Function app module) to transfer data from IoT Hub to my storage account:
'use strict';
// This function is triggered each time a message is revieved in the IoTHub.
// The message payload is persisted in an Azure Storage Table
var moment = require('moment');
module.exports = function (context, iotHubMessage) {
context.log('Message received: ' + JSON.stringify(iotHubMessage));
context.bindings.deviceData = {
"partitionKey": moment.utc().format('YYYYMMDD'),
"rowKey": moment.utc().format('hhmmss') + process.hrtime()[1] + '',
"message": JSON.stringify(iotHubMessage)
};
context.done();
};
But in my storage table it appears as a single row rather than being split into columns (as shown in the storage explorer.
How do I get it in columns for ts, timeToConnect, batLevel and vbat?
source to share
How do I get it in columns for ts, timeToConnect, batLevel and VBAT?
To get these attributes as separate columns in a table, you will need to filter the object and store it separately (currently you are just converting the whole object to a string and keep that string).
Please try the following code:
module.exports = function (context, iotHubMessage) {
context.log('Message received: ' + JSON.stringify(iotHubMessage));
var deviceData = {
"partitionKey": moment.utc().format('YYYYMMDD'),
"rowKey": moment.utc().format('hhmmss') + process.hrtime()[1] + '',
};
Object.keys(iotHubMessage).forEach(function(key) {
deviceData[key] = iotHubMessage[key];
});
context.bindings.deviceData = deviceData;
context.done();
};
Please note that I have not tried to execute this code so that it may contain some errors.
source to share