PowerShell展平嵌套JSON并将其转换为CSV

PowerShell展平嵌套JSON并将其转换为CSV,json,powershell,powershell-2.0,powershell-3.0,powershell-4.0,Json,Powershell,Powershell 2.0,Powershell 3.0,Powershell 4.0,我在下面有一个JSON文件。我正在尝试将其展平并转换为CSV { "tempid": "template_86CE6E3BE3AD4EAB95727BCBFAD6A83C", "auid": "audit_00006F5D7A114CE59AD572E3E878E726", "created_at": "2017-01-12T08:54:48.835Z", "Dateat": "2019-04-26T14:24:09.496Z", "Datefrom": { "sco

我在下面有一个JSON文件。我正在尝试将其展平并转换为CSV

{
  "tempid": "template_86CE6E3BE3AD4EAB95727BCBFAD6A83C",
  "auid": "audit_00006F5D7A114CE59AD572E3E878E726",
  "created_at": "2017-01-12T08:54:48.835Z",
  "Dateat": "2019-04-26T14:24:09.496Z",
  "Datefrom": {
    "score": 64,
    "duration": 1754,
    "space": {
      "device_id": "88888888888888",
      "owner": "John Paul"
  },
  "header_items": [
    {
      "item_id": "357085FF-B66A-4C28-B9D",
      "children": "66f7893245d45-ea77-0020"
    },
    {
      "parent_id": "357949D",
      "item_id": "f3789245d40-ea7o89797a66",
      "label": "Audit Title",
      "options": "@{is_mandatory=False}",
      "responses": "@{text=}"
    }
  ],
  "items": [  
    {
      "parent_id": "81C1FFE",
      "item_id": "B9CD2607897898-427898",
      "label": "TURN LEFT.",
      "type": "category"
    },
    {
      "parent_id": "456487k78978578",
      "item_id": "687fgfgfd",
      "label": "ANY RUBBISH?"
    }
  ]
}

我尝试了下面的代码,但发现错误“header\u item”找不到。我想它平坦成csv文件

Get-Content C:\can\Test\XY.json -Raw |
    ConvertFrom-Json | 
    Select -Expand header_items |
    Select -Expand items |
    Export-Csv C:\can\Test\XY.csv -NoTypeInformation

json提供的第一件事是格式错误(缺少括号),因此我假设它:

{
    "tempid": "template_86CE6E3BE3AD4EAB95727BCBFAD6A83C",
    "auid": "audit_00006F5D7A114CE59AD572E3E878E726",
    "created_at": "2017-01-12T08:54:48.835Z",
    "Dateat": "2019-04-26T14:24:09.496Z",
    "Datefrom": {},
    "score": 64,
    "duration": 1754,
    "space": {
        "device_id": "88888888888888",
        "owner": "John Paul"
    },
    "header_items": [
        {
            "item_id": "357085FF-B66A-4C28-B9D",
            "children": "66f7893245d45-ea77-0020"
        },
        {
            "parent_id": "357949D",
            "item_id": "f3789245d40-ea7o89797a66",
            "label": "Audit Title",
            "options": "@{is_mandatory=False}",
            "responses": "@{text=}"
        }
    ],
    "items": [
        {
            "parent_id": "81C1FFE",
            "item_id": "B9CD2607897898-427898",
            "label": "TURN LEFT.",
            "type": "category"
        },
        {
            "parent_id": "456487k78978578",
            "item_id": "687fgfgfd",
            "label": "ANY RUBBISH?"
        }
    ]
}
使用
Export Csv
PowerShell时的第二件事是使用第一个对象来确定Csv头,因此您必须列出所有对象的所有属性

$sourceFilePath = "C:\can\Test\XY.json"
$json = Get-Content $sourceFilePath -Raw | ConvertFrom-Json
$all = @( ($json.header_items | Select-Object *, @{Name = 'ItemType'; Expression = { 'HeaderItem' } }) ) + ($json.items | Select-Object *, @{Name = 'ItemType'; Expression = { 'Item' } })
$properties = ($all | ForEach-Object { $_ | Get-Member -MemberType NoteProperty}) | Select-Object -Unique -ExpandProperty Name
$destinationFilePath = "C:\can\Test\XY.jcsv"
$all | Select-Object -Property $properties | Export-Csv -NoTypeInformation -Path $destinationFilePath


关于,

“header\u items”
显示尾随
s
,但您的代码显示单数。。。[咧嘴笑]这是什么版本的时髦?您已列出2、3和4。。。并且应该只列出您需要使用的最低版本。当您“展平”json文件时,您希望如何处理重复的标题。。。示例:
parent\u id
@jrider。是的,我想复制标题,如items.parent\u id和header\u items.parent\u id