【问题标题】:Trying to convert complex JSON to CSV with Powershell尝试使用 Powershell 将复杂的 JSON 转换为 CSV
【发布时间】:2021-01-01 11:31:50
【问题描述】:

我是 PowerShell 的新手,我正在尝试将此 JSON 文件转换为 CSV。

使用一些像这样的简单代码:

$sourceFilePath = "Records.json"
$destinationFilePath = "Records.csv"
((Get-Content -Path $sourceFilePath -Raw) | ConvertFrom-Json) | Export-CSV $destinationFilePath -NoTypeInformation

结果:

因为我正在获取 System.Object[],所以我认为我需要对 contact_ids、site_ids、名字别名和别名进行某种扩展或“ForEach”。我已经进行了一些搜索,但似乎无法理解给出的示例。此外,“名字别名”中的空格会增加复杂性。

我无法控制源 JSON。以下是一个例子:

[
  {
    "mdm id" : "947b2a12-3aac-480a-a0bf-626aff106f48",
    "first name" : "Claude",
    "last name" : "Bones",
    "contact_ids" : [
      "CTP70499"
    ],
    "site_ids" : [
      "5015"
    ],
    "first name aliases" : [
      "Claude"
    ],
    "aliases" : [
      "Claude Bones"
    ],
    "createdDate" : "2020-06-03T19:59:08Z",
    "updatedDate" : "2020-06-03T20:48:27Z",
    "veevaID" : "V0B000000000569"
  },
  {
    "mdm id" : "bce21b05-0b28-4ebb-a34d-761c1a397821",
    "first name" : "Daniel",
    "last name" : "Smith",
    "contact_ids" : [
      "CTP699"
    ],
    "site_ids" : [
      "5015"
    ],
    "first name aliases" : [
      "Dan",
      "Danial",
      "Danne",
      "Danny",
      "Daniel"
    ],
    "aliases" : [
      "Daniel Smith"
    ],
    "createdDate" : "2020-06-03T19:59:08Z",
    "updatedDate" : "2020-06-03T20:48:27Z",
    "veevaID" : "V0B000000000566"
  }
]

【问题讨论】:

  • 在每个属性有多个值的情况下,您是否只想用逗号将它们连接起来?
  • @DougMaurer 是的,这对我有用

标签: json powershell csv


【解决方案1】:

数组的值需要组合成一个值。不管它可能有多少值,如果它在 json 中指定为数组[],则需要对其进行操作。互联网上有几篇文章和自定义函数。你的例子可以用这段代码来处理。

$JSONdata = @'
[
  {
    "mdm id" : "947b2a12-3aac-480a-a0bf-626aff106f48",
    "first name" : "Claude",
    "last name" : "Bones",
    "contact_ids" : [
      "CTP70499"
    ],
    "site_ids" : [
      "5015"
    ],
    "first name aliases" : [
      "Claude"
    ],
    "aliases" : [
      "Claude Bones"
    ],
    "createdDate" : "2020-06-03T19:59:08Z",
    "updatedDate" : "2020-06-03T20:48:27Z",
    "veevaID" : "V0B000000000569"
  },
  {
    "mdm id" : "bce21b05-0b28-4ebb-a34d-761c1a397821",
    "first name" : "Daniel",
    "last name" : "Smith",
    "contact_ids" : [
      "CTP699"
    ],
    "site_ids" : [
      "5015"
    ],
    "first name aliases" : [
      "Dan",
      "Danial",
      "Danne",
      "Danny",
      "Daniel"
    ],
    "aliases" : [
      "Daniel Smith"
    ],
    "createdDate" : "2020-06-03T19:59:08Z",
    "updatedDate" : "2020-06-03T20:48:27Z",
    "veevaID" : "V0B000000000566"
  }
]
'@ | ConvertFrom-Json

$JSONdata | foreach {
    $record = [ordered]@{}
    foreach($property in $_.psobject.Properties)
    {
        if($property.value -is [string])
        {
            $record.Add($property.name,$property.value)
        }
        else
        {
            $record.Add($property.name,($property.value -join ', '))
        }
    }
    [PSCustomObject]$record
} | ConvertTo-Csv -NoTypeInformation

输出

"mdm id","first name","last name","contact_ids","site_ids","first name aliases","aliases","createdDate","updatedDate","veevaID"
"947b2a12-3aac-480a-a0bf-626aff106f48","Claude","Bones","CTP70499","5015","Claude","Claude Bones","2020-06-03T19:59:08Z","2020-06-03T20:48:27Z","V0B000000000569"
"bce21b05-0b28-4ebb-a34d-761c1a397821","Daniel","Smith","CTP699","5015","Dan, Danial, Danne, Danny, Daniel","Daniel Smith","2020-06-03T19:59:08Z","2020-06-03T20:48:27Z","V0B000000000566"

只需将其更改为Export-Csv

【讨论】:

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