【问题标题】:Convert JSON to Csv columns in Powershell在 Powershell 中将 JSON 转换为 Csv 列
【发布时间】:2020-02-18 23:13:54
【问题描述】:

我想将我的 JSON 转换为以下 CSV 输出。我正在努力将“结果”数据解析为两列。

CSV 输出:

TIMESTAMP, VALUE,
1581292800000, 270,
1581292860000, 347

我的 JSON 输出如下:

$JSON =
        {
        "value":  [
                      {
                          "fields":  [
                                         "@{label=DateTime; field=eventTimestamp; type=date; aggregation=series}",
                                         "@{label=Count; field=metrics.End User Response Time (ms); type=integer; aggregation=count}"
                                     ],
                          "results":  [
                                          "1581292800000 270",
                                          "1581292860000 347",
                                          "1581292920000 348",
                                          "1581292980000 401",
                                          "1581293040000 435",
                                          "1581293100000 413",
                                          "1581293160000 466",
                                          "1581293220000 445",
                                          "1581293280000 450",
                                          "1581293340000 488",
                                          "1581293400000 470",
                                          "1581293460000 450",
                                          "1581293520000 440",
                                          "1581293580000 435",
                                          "1581293640000 403",
                                          "1581293700000 472",
                                          "1581293760000 392",
                                          "1581293820000 398",
                                          "1581293880000 357",
                                          "1581293940000 356",
                                          "1581294000000 361",
                                          "1581294060000 339",
                                          "1581294120000 373",
                                          "1581294180000 340",
                                          "1581294240000 329",
                                          "1581294300000 327",
                                          "1581294360000 307",
                                          "1581294420000 282",
                                          "1581294480000 315"
                                      ],
                          "moreData":  false,
                          "schema":  "browserrecord"
                      }
                  ],
        "Count":  1
    }

以下代码提取结果,但 TIMESTAMP 和 VALUE 都在同一列中。

$Output1 = ConvertTo-Json $JSON
$Output2 = ConvertFrom-Json $Outage2 | Select -ExpandProperty 'value' |  Select -ExpandProperty 'results' 

任何帮助将不胜感激。

干杯!

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    到目前为止,您已经成功地创建了一个字符串数组 $output2,其中包含 json 文档中所有以空格分隔的字符串值 - 到目前为止,一切都很好!

    我们现在可以使用-split 运算符将两个“列”拆分为单独的字符串:

    $results = $output2 |ForEach-Object {
        $ts,$value = -split $_
    
        [pscustomobject]@{
            TIMESTAMP = $ts
            VALUE = $value
        }
    }
    

    现在我们得到了一个对象数组,其中两个值被分隔为适当命名的属性TIMESTAMPVALUE,我们可以使用Export-Csv

    $results |Export-Csv .\results.csv -NoTypeInformation
    

    【讨论】:

    • 同意。 OP 的好榜样,完成了 90% 的工作!
    【解决方案2】:

    只需按空格分割值:

    (ConvertFrom-Json $JSON).value.results | `
      % { $pair = $_ -split ' '; [PSCustomObject]@{Timestamp=$pair[0]; Value=$pair[1]}} `
      | ConvertTo-Csv -NoTypeInformation
    

    【讨论】:

      【解决方案3】:

      拆分结果。您可以将其通过管道传输到 export-csv。

      '1581292800000 270',
      '1581292860000 347',
      '1581292920000 348' | foreach { 
        $timestamp,$value = -split $_ 
        [pscustomobject]@{Timestamp = $timestamp
                              Value = $value} 
      }
      
      
      Timestamp     Value
      ---------     -----
      1581292800000 270
      1581292860000 347
      1581292920000 348
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-23
        • 2021-01-08
        • 2021-03-31
        • 2017-09-18
        • 2017-09-21
        • 1970-01-01
        • 2015-08-09
        • 2021-01-08
        相关资源
        最近更新 更多