none
Usage API not supplying empty tags property in JSON when a resource has no tags

    Question

  • Has anyone had issues with getting tags from the usage API lately? I am using the Azure Usage and Billing API example on git hub (https://github.com/Azure-Samples/billing-dotnet-usage-api) to retrieve usage and billing data for my subscriptions. Everything works fine but I am seeing in the json response from the usage API that sometimes the tag is not supplied at all so I am getting a null reference error when trying to store tags to the database on Azure resources that contain no tags.

    JSON Response on a resource WITH tags:

    {  
             "id":"/subscriptions/blahblah/providers/Microsoft.Commerce/UsageAggregates/Daily_BRSDT_20170307_0000",
             "name":"Daily_BRSDT_20170307_0000",
             "type":"Microsoft.Commerce/UsageAggregate",
             "properties":{  
                "subscriptionId":"blbahblah",
                "usageStartTime":"2017-03-06T00:00:00+00:00",
                "usageEndTime":"2017-03-07T00:00:00+00:00",
                "meterName":"Free App Service",
                "meterCategory":"Azure App Service",
                "unit":"Apps",
                "instanceData":"{\"Microsoft.Resources\":{\"resourceUri\":\"/subscriptions/blahblah/resourceGroups/blahblah/providers/Microsoft.Web/sites/blahblah\",\"location\":\"centralus\",\"tags\":{\"CreatedBy\":\"Blahblah\",\"Project\":\"blahblah\"},\"additionalInfo\":{\"AppServicePlanUri\":\"/subscriptions/blahblah/resourceGroups/blahblah/providers/Microsoft.Web/blahblah/blahblah\"}}}",
                "meterId":"c0f5cb45-6fb1-41c9-8545-72ad400d9da4",
                "infoFields":{  

                },
                "quantity":0.004032
             }
          },

    JSON response on resource WITHOUT tags:    

     {  
             "id":"/subscriptions/blahblah/providers/Microsoft.Commerce/UsageAggregates/Daily_BRSDT_20170307_0000",
             "name":"Daily_BRSDT_20170307_0000",
             "type":"Microsoft.Commerce/UsageAggregate",
             "properties":{  
                "subscriptionId":"blahblah",
                "usageStartTime":"2017-03-06T00:00:00+00:00",
                "usageEndTime":"2017-03-07T00:00:00+00:00",
                "meterName":"Free App Service",
                "meterCategory":"Azure App Service",
                "unit":"Apps",
    "instanceData":"{\"Microsoft.Resources\":{\"resourceUri\":\"/subscriptions/cblah/resourceGroups/blah/providers/Microsoft.Web/sites/blah\",\"location\":\"australiaeast\",\"additionalInfo\":{\"AppServicePlanUri\":\"/subscriptions/blah/resourceGroups/blah/providers/Microsoft.Web/blah/blah\"}}}",
                "meterId":"c0f5cb45-6fb1-41c9-8545-72ad400d9da4",
                "infoFields":{  

                },
                "quantity":0.004032
             }
          },

    Class is setup according to the git hub example. 

    public class MicrosoftResourcesDataType
            {
                public string resourceUri { get; set; }

                public IDictionary<string, string> tags { get; set; }
              
                public IDictionary<string, string> additionalInfo { get; set; }

                public string location { get; set; }

                public string partNumber { get; set; }

                public string orderNumber { get; set; }
            }

    My code is tripping when a resource has no tags because the "tags" dictionary is never initialised, therefore I get a null reference error. How do I assign a default value or null value when this occurs so my code continues to loop through each record as it should?


    • Edited by Amanda Gaffey Monday, March 13, 2017 2:03 AM
    • Moved by Sapna Girish Monday, March 13, 2017 2:37 PM related to Partner Center API
    Monday, March 13, 2017 2:02 AM

Answers

  • Hi,

    Based on that sample project downloaded from GitHub, you can extract the tags like this to avoid the null object issue:

    UsagePayload payload = JsonConvert.DeserializeObject<UsagePayload>(usageResponse);
    List<UsageAggregate> value = payload.value;
    foreach (UsageAggregate usage in value)
    {
        Properties properties = usage.properties;
        if (null != properties.instanceDataRaw)
        {
            if (null != properties.InstanceData)
            {
                InstanceDataType instanceData = properties.InstanceData;
                if (null != instanceData.MicrosoftResources.tags)
                {
                    IDictionary<string, string> dict = instanceData.MicrosoftResources.tags;
                    foreach (var pair in dict)
                    {
                        Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
                    }
                }
            }
        }
        
    }

    Best regards,

    Patrick Liang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 14, 2017 10:46 AM
    Moderator

All replies

  • Hi Amanda,

    Recently, I’m also look into this issue, just a note that, I will come back once there is any finding on my side.

    It would also be appreciated if you can keep updating this thread once there is any thought from you.

    Best regards,

    Patrick Liang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 14, 2017 2:03 AM
    Moderator
  • Hi,

    Based on that sample project downloaded from GitHub, you can extract the tags like this to avoid the null object issue:

    UsagePayload payload = JsonConvert.DeserializeObject<UsagePayload>(usageResponse);
    List<UsageAggregate> value = payload.value;
    foreach (UsageAggregate usage in value)
    {
        Properties properties = usage.properties;
        if (null != properties.instanceDataRaw)
        {
            if (null != properties.InstanceData)
            {
                InstanceDataType instanceData = properties.InstanceData;
                if (null != instanceData.MicrosoftResources.tags)
                {
                    IDictionary<string, string> dict = instanceData.MicrosoftResources.tags;
                    foreach (var pair in dict)
                    {
                        Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
                    }
                }
            }
        }
        
    }

    Best regards,

    Patrick Liang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 14, 2017 10:46 AM
    Moderator
  • Hi Amanda,

    Just want to know that if my reply can be helpful to you?

    Best regards,

    Patrick Liang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 15, 2017 2:03 AM
    Moderator
  • Hi Patrick,

    Sorry for the late reply. I was away from the office for the last two days. Thank you for your post. I will give it a go and let you know whether or not it solved my issue.

    Kind regards, Amanda

    Friday, March 17, 2017 12:25 AM
  • Hi Amanda,

    Any progress now?

    Best regards,

    Patrick


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, March 20, 2017 8:57 AM
    Moderator
  • Thank you Patrick! Your information solved my problem. All working now :)
    Monday, March 20, 2017 11:53 PM