locked
DataContractJsonSerializer C# REST API custom log RRS feed

  • Question

  • Hi,

    I am retrieving a "custom log" from "Azure Log Analytics" with C# code (via REST GET API) using DataContractJsonSerializer.
    The data comes back but the serializer is not parsing the "rows" json/section.
    In the code block below, there are 3 sections:
    1. The json data coming back from Azure Log Analytics
    2. The DataContracts/classes to hold the parsed json
    3. The C# code to put the json into the DataContracts/classes

    The DataContractJsonSerializer.ReadObject call mostly works except the "rows" data does not get populated.
    It instantiates the correct number of row records but each value in the RowsData class (i.e. variables named TenantId, SourceSystem, MG) are all null (btw, there are many more variables but I condensed to write this forum entry).
    I can see the data is coming back from the REST call, it just seems the DataContractJsonSerializer does not parse it based on the DataContracts/classes that are defined.
    Any advice or thoughts would be greatly appreciated.
    Thank you for your time.

    //Section 1 - sample data coming back from Azure Log Analytics
    
    {
      "tables": [
        {
          "name": "PrimaryResult",
          "columns": [
            {
              "name": "TenantId",
              "type": "string"
            },
            {
              "name": "SourceSystem",
              "type": "string"
            },
            {
              "name": "MG",
              "type": "string"
            }
          ],
          "rows": [
            [
              "12345678-2404-9999-b01e-000000000000",
              "RestAPI",
              ""
            ],
            [
              "87654321-2404-1111-b01e-000000000000",
              "RestAPI",
              ""
            ]
          ]
        }
      ]
    } 
    
    //Section 2 - the DataContracts/classes to hold the parsed json
    
    [ DataContract ] public class ColumnsData {
      [ DataMember ]
      public string
      name { get; set; }
      [ DataMember ]
      public string
      type { get; set; }
    } 
    
    [ DataContract ] public class RowsData {
      [ DataMember ]
      public string
      TenantId { get; set; }
      [ DataMember ]
      public string
      SourceSystem { get; set; }
      [ DataMember ]
      public string
      MG { get; set; }
      [ DataMember ]
    } 
    
    [ DataContract ] public class AzureLogAnalyticsTablesData {
      [ DataMember ]
      public string
      name { get; set; }
      [ DataMember ]
      public List<ColumnsData>
      columns; [ DataMember ]
      public List<RowsData>
      rows;
    } 
    
    [ DataContract ] public class AzureLogAnalyticsData {
      [ DataMember ]
      public List<AzureLogAnalyticsTablesData>
      tables;
    }; 
    
    //Section 3 - the C# code to populate the DataContracts/classes with the json data
    
    //l_MemoryStream holds the json data
    
    DataContractJsonSerializer l_DataContractJsonSerializer = 
    	new DataContractJsonSerializer(typeof(AzureLogAnalyticsData));
    
    AzureLogAnalyticsData l_AzureLogAnalyticsData = 
    	(AzureLogAnalyticsData)l_DataContractJsonSerializer.ReadObject(l_MemoryStream); 
    
    foreach (AzureLogAnalyticsTablesData l_AzureLogAnalyticsTablesData in l_AzureLogAnalyticsData.tables) {
    	foreach (RowsData l_RowsData in	l_AzureLogAnalyticsTablesData.rows) {
    		//ISSUE: all the variables in RowsData are null even though I can see the values in the json string
    	}
    }


    Anthony LaMark

    Wednesday, May 27, 2020 11:29 PM