【问题标题】:Creating a CSV file with proper columns & rows from json file using Powershell使用 Powershell 从 json 文件中创建具有适当列和行的 CSV 文件
【发布时间】:2021-12-24 04:52:25
【问题描述】:

尝试了几个命令,例如Get-Content jsonfile.json | Out-String | ConvertFrom-Json,但没有任何效果。

如何将名称、版本、id、作者、描述、部署日期转换为列和新行。

Json 文件示例:

[
  {
    "name": "ABC",
    "version": "1.0.4",
    "id": "f5e2b3ed-ac07",
    "customer": "Abby",
    "description": "An example- \n\nDate:\nFrom:\nTo:\nSubject:\nBody:\nAttachments\n\n\n",
    "deploymentDate": 11102021140122,
    "deployedBy": "Kathy Hick",
    "signDetails": {
      "signStatus": "notSigned",
      "signedBy": null,     
  },
  {
    "name": "This_is_example",
    "version": "1.0.4",
    "id": "992e-da106ee7fcff",
    "publisher": "Customer",
    "description": "",
    "deploymentDate": 1634762732781,
    "deployedBy": "joehannible01",
    "signDetails": {
      "signStatus": "notSigned",
      "signedBy": null,     
  } 
  ]

【问题讨论】:

  • 您好,欢迎来到 SO。在我们继续之前,您应该立即使用Tour 并至少阅读How to Askminimal reproducible example 的帮助主题,以设定您对SO 的期望。无论如何 - 当您发布代码或示例数据时,您应该将它们格式化为代码。提前致谢。
  • 您的 JSON 数据无效。如果您使用有效的 JSON,它只会将 JSON 字符串传输到 ConvertFrom-Json 并将其传输到 Export-CsvConvertTo-Csv

标签: arrays powershell csv json.net


【解决方案1】:
$properties=@('Name','Version','Id','Publisher','description','deploymentDate','deployedBy')

(Get-Content -Path C:\Temp\testing.json -Raw  | ConvertFrom-Json) | Select-Object -Property $properties | ConvertTo-Csv -NoTypeInformation | Set-Content -Path C:\Temp\convertcsvtable.csv

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2019-05-31
  • 2019-08-04
  • 2016-11-21
  • 2017-01-29
  • 2017-10-22
  • 2016-07-18
  • 2021-02-20
  • 1970-01-01
相关资源
最近更新 更多