locked
Export Table Data to JSON via Azure Storage SDK RRS feed

  • Question

  • Hi,

    I'm looking to export the contents of a table in JSON format and then save that JSON to one or more files.

    I see that we can have the table return JSON using: tableClient.DefaultRequestOptions.PayloadFormat = TablePayloadFormat.Json;

    I also see how to deserialize the JSON into an array of objects using something like this to get an array of CustomerEntity:

    IQueryable<CustomerEntity> query = from customer in table.CreateQuery<CustomerEntity>()
    where string.Compare(customer.PartitionKey, "I") >= 0 &&
    string.Compare(customer.PartitionKey, "X") <= 0 &&
    customer.Rating >= 2
    select customer;
    
    CustomerEntity[] customers = query.ToArray();

    But what if I don't want the results as CustomerEntity objects, I just want the raw JSON?

    The CloudTable.CreateQuery method requires a type that inherits from ITableEntity...

    I guess I could switch from using the Azure Storage SDK client to an HTTP client and query via OData, but I'd prefer a solution within the Azure Storage SDK...

    Thanks,

    Aron

    Sunday, January 18, 2015 5:34 PM

Answers

  • Hi Aron,

    It seems that you don't use C# client library to get the JSON data before it gets case to Entities. If you use REST API (http://msdn.microsoft.com/en-us/library/azure/dd179421.aspx ), I think you can get the JSON data directly.

    Regards,

    Will


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Aron F Monday, January 26, 2015 2:10 PM
    Sunday, January 25, 2015 12:55 PM

All replies

  • Hi Aron,

    Thanks for your posting!

    Base on my experience, we could set the entities as JSON data using "PayloadFormat " in SDK, such as this blog(http://blogs.msdn.com/b/windowsazurestorage/archive/2013/12/05/windows-azure-tables-introducing-json.aspx ). But if you don't want to use this, I think the Azure Table REST API is the best choice for this requirement. You could only set the content-type into request, you could got the json results.

    http://msdn.microsoft.com/en-us/library/azure/dd179421.aspx

    Regards,

    Will


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 19, 2015 9:00 AM
  • Thanks Will,

    Here is a more complete code snippet. As you can see, I have the payload set to JSON.

    const string customersTableName = "Customers";
    string connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", accountName, accountKey);
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
    
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    // Values supported can be AtomPub, Json, JsonFullMetadata or JsonNoMetadata with Json being the default value
    tableClient.DefaultRequestOptions.PayloadFormat = TablePayloadFormat.Json;
    
    // Create the Customers table
    CloudTable table = tableClient.GetTableReference(customersTableName);
    table.CreateIfNotExists();
    
    // Insert a couple of customers into the Customers table
    foreach (CustomerEntity customer in CustomerEntity.GetCustomersToInsert())
    {
    	table.Execute(TableOperation.Insert(customer, echoContent: false));
    }
    
    // The response have a payload format of JSON no metadata and the 
    // client library will map the properties returned back to the CustomerEntity object
    IQueryable<CustomerEntity> query = from customer in table.CreateQuery<CustomerEntity>()
    								   where string.Compare(customer.PartitionKey, "I") >= 0 &&
    								   string.Compare(customer.PartitionKey, "X") <= 0 &&
    								   customer.Rating >= 2
    								   select customer;
    
    CustomerEntity[] customers = query.ToArray();

    However, the way the query is set up it automatically casts the results as CustomerEntity. That's the challenge - How to get the JSON payload before it gets cast to CustomerEntity...

    Thanks,

    Aron

    Monday, January 19, 2015 2:14 PM
  • Hi Aron,

    It seems that you don't use C# client library to get the JSON data before it gets case to Entities. If you use REST API (http://msdn.microsoft.com/en-us/library/azure/dd179421.aspx ), I think you can get the JSON data directly.

    Regards,

    Will


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Aron F Monday, January 26, 2015 2:10 PM
    Sunday, January 25, 2015 12:55 PM
  • Thanks Will. I came to the same conclusion.
    Monday, January 26, 2015 2:11 PM
  • Hi, Aron

    How did you solve it at last, could you share you solution here?

    Thanks.

    Wednesday, October 28, 2015 9:21 AM