title: How to fetch the last item in a JSON in C#
body: <p>I'm trying to get a single value out of a JSON response from Azure Rest API. </p>
  
  <p>Below is my code where I was able to get to final array but unable to fetch the last item from this array.</p>
  
  <pre><code>            string token = await GetAccessToken(GMETenantID, GMEClientID, GMEAppKey);
  
              var httpClient = new HttpClient
              {
                  BaseAddress = new Uri("https://management.azure.com/subscriptions/")
              };
              string URI = $"/subscriptions/{SubscriptionGUID}/providers/Microsoft.Storage/storageAccounts?api-version=2019-06-01";
  
              httpClient.DefaultRequestHeaders.Remove("Authorization");
              httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
              HttpResponseMessage response = await httpClient.GetAsync(URI).ConfigureAwait(false);
  
              var HttpsResponse = await response.Content.ReadAsStringAsync();
              dynamic Result = JsonConvert.DeserializeObject&lt;object&gt;(HttpsResponse);
  
              foreach (dynamic item in Result["value"])
              {
                  string StorageID = item.id;
                  string StorageAccountName = item.name;
                  string ResourceType = item.type;
  
                  int CharsToRemove = 12 + ResourceType.Length + StorageAccountName.Length;
  
                  string ResourceGroupName = StorageID.Remove(0,67);
                  ResourceGroupName = ResourceGroupName.Remove(ResourceGroupName.Length - CharsToRemove);
  
                  string StartTime = DateTime.Now.AddDays(-7).ToString("yyyy-MM-ddTHH:mm:ss") + "Z"; ;
                  string EndTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + "Z";
  
                  var BlobhttpClient = new HttpClient
                  {
                      BaseAddress = new Uri("https://management.azure.com/subscriptions/")
                  };
  
                  string BlobURI = $"/subscriptions/{SubscriptionGUID}/resourceGroups/{ResourceGroupName}/providers/{ResourceType}/{StorageAccountName}/blobServices/default/providers/microsoft.insights/metrics?timespan={StartTime}/{EndTime}&amp;aggregation=average&amp;metricnames=BlobCapacity&amp;api-version=2018-01-01";
                  BlobhttpClient.DefaultRequestHeaders.Remove("Authorization");
                  BlobhttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                  HttpResponseMessage Blobresponse = await httpClient.GetAsync(BlobURI).ConfigureAwait(false);
                  var BlobHttpsResponse = await Blobresponse.Content.ReadAsStringAsync();
  
                  dynamic value = JObject.Parse(BlobHttpsResponse);
  
                  foreach (dynamic List1 in value["value"])
                  {
                      foreach (dynamic List2 in List1["timeseries"])
                      {
                          foreach (dynamic List3 in List2["data"])
                          {
                              Console.WriteLine((long?)List3["average"]);
                          }
                      }
                  }
              }
              Console.ReadLine();
  </code></pre>
  
  <p>I also feel there must be an easier way to get this one value out instead of using 3 Foreach loops.</p>
  
  <p>HttpsResponse: (There are a 168 timestamp and average values in the actual response but i've put only 3 values)
  </p>
  
  <pre><code>{
    "cost": 0,
    "timespan": "2020-05-22T03:04:46Z/2020-05-29T03:04:46Z",
    "interval": "PT1H",
    "value": [
      {
        "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroupName/providers/Microsoft.Storage/storageAccounts/MyAstorageAccountName/blobServices/default/providers/Microsoft.Insights/metrics/BlobCapacity",
        "type": "Microsoft.Insights/metrics",
        "name": {
          "value": "BlobCapacity",
          "localizedValue": "Blob Capacity"
        },
        "displayDescription": "The amount of storage used by the storage account's Blob service in bytes.",
        "unit": "Bytes",
        ".timeseries": [
          {
            "metadatavalues": [],
            "data": [
              {
                "timeStamp": "2020-05-22T03:04:00Z",
                "average": 586904516140.0
              },
              {
                "timeStamp": "2020-05-22T04:04:00Z",
                "average": 587058965680.0
              },
              {
                "timeStamp": "2020-05-22T05:04:00Z",
                "average": 587058965680.0
              }
            ]
          }
        ],
        "errorCode": "Success"
      }
    ],
    "namespace": "Microsoft.Storage/storageAccounts/blobServices",
    "resourceregion": "eastus2"
  }
  </code></pre>
  
  <p>All I want is the number 587058965680 which is the last value of the JSON and the most recent timestamp.</p>
  
  <p>Pretty much I want to get exact result the way I'm getting in below powershell code
  PowerShell works like a charm in 4 lines but I had to write so many lines in C#</p>
  
  <pre><code>$StorageAccounts = Get-AzStorageAccount -ResourceGroupName "My ResourceGroupName"
  
  $StorageAccountID = $StorageAccounts.ID + "/blobServices/default"
  $Start = (get-date).AddDays(-7)
  $End = get-date
  
  $Metric = Get-AzMetric -ResourceId $StorageAccountID -MetricName "BlobCapacity" -StartTime $Start -EndTime $End  -WarningAction Ignore
  
  $ContainerSize = ($Metric.Data | sort-object -Descending Average | Select-Object -First 1).Average/1024/1024/1024
  </code></pre>