azure table how to create the table and add json data using javascript

I am trying to write a javascript function that will do two things
1st create a table in azure if it doesnt already exist

and 2nd add a row to the table I just created

I dont know how to do the following:
  1 - pass the name of the table to the azure function that creates the table I put the value in a variable TName and passed that to the function , but I could not get this to work and I had to hard code the name as a temp fix

  2 - how to create the azure table with the data that is in the json

I have tried to look for examples and I just need a very simple example that I can then make the appropriate changes ...

below i where I have got to and any help is really appreciated

example of the json data I am passing to the function:

Open in new window

// here is the function I am trying to build
function (context, iJsonData) {
    var azureStorage = require('azure-storage');    
    var AzureEnv = process.env.test_storage;

	var tableService = azureStorage.createTableService(process.env.test_storage);

// get the season ticket data to be loaded  
    var ClientData = iJsonData; 

	var TName = "MyTestTable";

// how to pass TName to the function createTableIfNotExists ???
tableService.createTableIfNotExists('TEST1', function(error, result, response) {
  if(!error) { 
            context.log("Table "+TName+ " created since it did not exist");
            context.log("Table "+TName+ " already exists");

          context.log("result ");
          context.log("response ");
          context.log("Table "+TName+ " had an error while creating the table");
          context.log("result ");
          context.log("response ");

//How to define the structure of the azure table????

// How to insert the data that is in the json to the table ???


Open in new window

Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
how to pass TName to the function createTableIfNotExists ???
tableService.createTableIfNotExists(TName, function(error, result, response) {

Open in new window

How to define the structure of the azure table????
Table: A table is a collection of entities. Tables don't enforce a schema on entities, which means a single table can contain entities that have different sets of properties. The number of tables that a storage account can contain is limited only by the storage account capacity limit.

How to insert the data that is in the json to the table ???
From what I read from here, the following should work :
var entGen = azure.TableUtilities.entityGenerator;
var task = {
  PartitionKey: entGen.String('hometasks'),
  RowKey: entGen.String('1'),
  description: entGen.String('take out the trash'),
  dueDate: entGen.DateTime(new Date(Date.UTC(2015, 6, 20))),
var account = {
 PartitionKey: entGen.String('accounts'),
 RowKey: entGen.String('1234'),
tableSvc.insertEntity(TName, account, function (error, result, response) {
    // Entity inserted

Open in new window

please read note about PartitionKey and RowKey on this page :
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.