嵌套Json至CSV powershell

时间:2019-03-07 09:59:44

标签: json powershell csv nested

我尝试自己解决问题,但是我对PS和Json的了解不足,无法适应我的问题。

任务似乎很简单,但是我通过Rest API获得的json结构对我来说有点愚蠢。

示例JSON(具有更多列,例如(test-id,test-Name),但在字段中的所有列上具有完全相同的逻辑):

     {
  "entities": [
    {
      "Fields": [
        {
          "Name": "test-id",
          "values": [
            {
              "value": "1851"
            }
          ]
        },
        {
          "Name": "test-name",
          "values": [
            {
              "value": "01_DUMMY"
            }
          ]
        }


      ],
      "Type": "run",
      "children-count": 0
    },
    {
      "Fields": [
        {
          "Name": "test-id",
          "values": [
            {
              "value": "1852"
            }
          ]
        },
        {
          "Name": "test-name",
          "values": [
            {
              "value": "02_DUMMY"
            }
          ]
        }
      ],
      "Type": "run",
      "children-count": 0
    }
  ],
  "TotalResults": 2
}

我尝试遵循PS脚本:

Get-Content $file -raw |
convertfrom-json  | select -ExpandProperty entities |
select -ExpandProperty Fields |
select -ExpandProperty Values |
Export-CSV $OutputFile -NoTypeInformation

但是我的CSV结果看起来像这样:

"value"
"1851"
"01_DUMMY"
"N"

我想收到以下结果:

test-id,test-Name,run,children-count
1851,01_DUMMY,run,0
1852,02_DUMMY,run,0

0 个答案:

没有答案