2

I am creating a list of objects in C#. I want to save them in proper JSON format on the go in the blob storage container. Currently I am saving them on my local drive and then upload to blob. Two problem I am facing: 1. Json format when I add new objects looks like:

[
  {
    "id": "1",
    "Name": "Peter",
    "Surname": "Pan"  
  }
] 
[
  {
    "id": "2",
    "Name": "Steve",
    "Surname": "Pan"  
  }
] 

How may I update my code to make them one array with comma separeted values?

[
 {
    "id": "1",
    "Name": "Peter",
    "Surname": "Pan"  
  },
 {
    "id": "2",
    "Name": "Steve",
    "Surname": "Pan"  
  }
]
  1. Is there a way to save to my blob without first saving the file on my local drive?
List<Obj> list= new List<Obj>();

list.Add(new Obj()
{               
Id = "1",               
Name = "Peter",            
Surname = "Pan"
});

// Serialize to JSON output
var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(list);     

// write string to file locally
System.IO.File.AppendAllText(@"people.json", serializedResult);

//Create or overwrite the "myblob" blob with the contents of a local file
using (var fileStream = System.IO.File.OpenRead(@"people.json"))
{
   await blockBlob.UploadFromStreamAsync(fileStream);
}

Json out has wrong format with new object created being a new array also how to upload this file without saving a copy on my local drive?

4
  • 1
    Is there a reason you are using a blob storage instead of an Azure table? I'm not sure what causes your JSON to appear as a list of lists, but I suggest switching to Json.NET and see if this reproduces. Commented Aug 15, 2019 at 10:31
  • I have just swapped to Json.NET and I am getting the same output. I have never used Azure tables that's why I was thinking to use blob. Commented Aug 15, 2019 at 10:44
  • Azure tables will allow you store the information without converting it to JSON and query it back by different properties. Let me know which approach you prefer and I will add some demo code as an answer. Commented Aug 15, 2019 at 11:14
  • Seems good I may try the tables. Is the way I create my object stay the same ? Commented Aug 15, 2019 at 12:03

1 Answer 1

1

You can use Azure Tables (Here is a great tutorial with examples) for your use case. Basically, you will save the data without converting it to JSON first and will be able to query and update specific data without re-uploading the whole file.

Based on the tutorial examples and your code, you'll need to do something like this:

CloudStorageAccount storageAccount = new CloudStorageAccount(
    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
        "<name>", "<account-key>"), true);

// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Get a reference to a table named "peopleTable"
CloudTable peopleTable = tableClient.GetTableReference("peopleTable");

await peopleTable.CreateIfNotExistsAsync();

TableBatchOperation batch = new TableBatchOperation();

batch.Add(TableOperation.InsertOrReplace(new Obj()
{               
    PartitionKey = "1",
    RowKey = "<SomeOtherIdentifier>",
    Name = "Peter",            
    Surname = "Pan"
}));

IList<TableResult> results = await peopleTable.ExecuteBatchAsync(batch);

// Do what you have to do with the results

A VERY important note: Your model (Obj in this case) must implement the interface ITableEntity, or simply derive from EntityEntity object. This is why your Obj has now the PartitionKey and the RowKey properties.

The combination of the two properties creates the unique identifier for each row in the Azure Table.

Let me know if you have any more questions. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.