Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing JSON data as columns in Azure table storage

How do a format my json data and/or change my function so that it gets stored as columns in Azure table storage?

I am sending a json string to the IoT hub:

{"ts":"2017-03-31T02:14:36.426Z","timeToConnect":"78","batLevel":"83.52","vbat":"3.94"}

I run the sample function (in the Azure Function App module) to transfer the data from the IoT hub into 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 shows up as a single string rather than getting split into columns (as seen in the storage explorer.

Azure storage explorer

How do I get it into columns for ts, timeToConnect, batLevel, and vbat?

like image 294
Mark Peterson Avatar asked Mar 31 '17 04:03

Mark Peterson


People also ask

Does Azure table support JSON?

While the ATOM protocol is supported for all versions of the Azure storage services, the JSON protocol is supported only for version 2013-08-15 and newer. JSON is the recommended payload format. JSON is supported for version 2013-08-15 and newer.

How do I make columns in Azure table Storage?

Windows Azure Table Storage doesn't actually have columns. Each entity (e.g. a Row) is simply a set of properties, with no fixed schema. If you're using a strongly-typed class to write to your table, then you just need to add your new property to that class.

Where does Azure store JSON files?

Any client-side language or library that works with string data in Azure SQL Database and Azure SQL Managed Instance will also work with JSON data. JSON can be stored in any table that supports the NVARCHAR type, such as a Memory-optimized table or a System-versioned table.


1 Answers

How do I get it into columns for ts, timeToConnect, batLevel, and vbat?

To get these attributes as separate columns in table, you would need to defalte the object and store them separately (currently you are just converting the entire object into string and storing 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 it may contain some errors.

like image 115
Gaurav Mantri Avatar answered Nov 15 '22 17:11

Gaurav Mantri