如何将Azure API中的JSON文件数据转换为CSV?

tvokkenx  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一个JSON文件,其中有如下所示的数据,我试图使用PowerShell脚本将其转换为CSV,但我得到的响应是意外的。我将如何在CSV列中正确获取时间戳,平均值和最大值的数据。

JSON data:

[
    {
        "cost":  44639,
        "timespan":  "2023-02-10T23:59:59Z/2023-03-13T23:59:59Z",
        "interval":  "PT1H",
        "value":  [
                      {
                          "id":  "/subscriptions/xxxx/resourceGroups/Automation-script/providers/Microsoft.Storage/storageAccounts/automationscript87c1/providers/Microsoft.Insights/metrics/UsedCapacity",
                          "type":  "Microsoft.Insights/metrics",
                          "name":  {
                                       "value":  "UsedCapacity",
                                       "localizedValue":  "Used capacity"
                                   },
                          "displayDescription":  "The amount of storage used by the storage account. For standard storage accounts, it\u0027s the sum of capacity used by blob, table, file, and queue. For premium storage accounts and Blob storage accounts, it is the same as BlobCapacity or FileCapacity.",
                          "unit":  "Bytes",
                          "timeseries":  [
                                             {
                                                 "metadatavalues":  [

                                                                    ],
                                                 "data":  [
                                                              {
                                                                  "timeStamp":  "2023-02-10T23:59:00Z"
                                                              },
                                                              {
                                                                  "timeStamp":  "2023-02-11T00:59:00Z"
                                                              },
                                                              {
                                                                  "timeStamp":  "2023-02-11T01:59:00Z"
                                                              },
                                                              {
                                                                  "timeStamp":  "2023-02-11T02:59:00Z", 
                                                                  "average" : 17044123
                                                              }
                                                 
                                                          ]
                                             }
                                         ],
                          "errorCode":  "Success"
                      }
                  ],
        "namespace":  "Microsoft.Storage/storageAccounts",
        "resourceregion":  "centralindia"
    },
    
    
        "cost":  44639,
        "timespan":  "2023-02-10T23:59:59Z/2023-03-13T23:59:59Z",
        "interval":  "PT1H",
        "value":  [
                      {
                          "id":  "/subscriptions/xxxxx/resourceGroups/VM-DavePlatform_group/providers/Microsoft.Compute/virtualMachines/AnotherTestVM/providers/Microsoft.Insights/metrics/Percentage CPU",
                          "type":  "Microsoft.Insights/metrics",
                          "name":  {
                                       "value":  "Percentage CPU",
                                       "localizedValue":  "Percentage CPU"
                                   },
                          "displayDescription":  "The percentage of allocated compute units that are currently in use by the Virtual Machine(s)",
                          "unit":  "Percent",
                          "timeseries":  [
                                             {
                                                 "metadatavalues":  [

                                                                    ],
                                                 "data":  [
                                                              {
                                                                  "timeStamp":  "2023-02-10T23:59:00Z"
                                                              },
                                                              {
                                                                  "timeStamp":  "2023-02-11T00:59:00Z"
                                                              },
                                                              {"timeStamp":  "2023-03-13T21:59:00Z",
                                                               "maximum" 35.16
                                                              }
                                                          ]
                                             }
                                         ],
                          "errorCode":  "Success"
                      }
                  ],
        "namespace":  "Microsoft.Compute/virtualMachines",
        "resourceregion":  "centralindia"
    } ]

Powershell脚本

# Load the JSON file
    $json = Get-Content -Raw -Path "C:\Users\$env:UserName\Downloads\All_Resources_Combined.JSON" | ConvertFrom-Json
    
    # Select the properties you want to keep and convert the values to string
    $json | Select-Object @{n='Id';e={$_.value.id}},
                                   @{n='LocalizedValue';e={$_.value.name.localizedValue}},
                                   @{n='Unit';e={$_.value.unit}},
                                   @{n='Timestamp';e={$_.value.timeseries.data | ForEach-Object {$_.timeStamp}}},
                                   @{n='Average';e={$_.value.timeseries.data | ForEach-Object {$_.average}}},
                                   @{n='Maximum';e={$_.value.timeseries.data | ForEach-Object {$_.maximum}}} |
            Export-Csv  "C:\Users\$env:UserName\Downloads\file.csv" -NoTypeInformation

csv data
我已经尝试为每个时间戳迭代我的PowerShell脚本,但我仍然没有获得每行的每个时间戳的数据,而是在一行中获得所有时间戳值。

8hhllhi2

8hhllhi21#

您需要对每个对象的.value.timeseries.data.timestamp属性值进行循环,以便为每个对象创建输出对象。
基于您的方法(如果性能是一个问题,可以优化):

Get-Content -Raw C:\Users\$env:UserName\Downloads\All_Resources_Combined.JSON | 
  ConvertFrom-Json | 
  ForEach-Object {
    foreach ($ts in $_.value.timeseries.data.timestamp) {
       $_ | 
         Select-Object @{n = 'Id'; e = { $_.value.id } },
           @{n = 'LocalizedValue'; e = { $_.value.name.localizedValue } },
           @{n = 'Unit'; e = { $_.value.unit } },
           @{n = 'Timestamp'; e = { $ts } },
           @{n = 'Average'; e = { $_.value.timeseries.data.average } } },
           @{n = 'Maximum'; e = { $_.value.timeseries.data.maximum } } } 
    }
  } # | Export-Csv ...

注意上面在属性访问表达式.value.timeseries.data.timestamp.value.timeseries.data.average.value.timeseries.data.maximum中使用了member-access enumeration

相关问题