我正在尝试使用Powershell将json文件转换为csv文件。我尝试转换的json文件具有多个嵌套数组。我想全部展开。以下是我正在使用的文件结构和代码:
这是我的feed.json文件的快照:
{
"result": {
"problems": [
{
"id": "AHR157689",
"displayName": "YOCETJE",
"impact": "STRUCTURE",
"status": "OPEN",
"tagsOfEntities": [],
"ranked": [
{
"entityId": "843675746378564876",
"entityName": "HGFUTGYJDH",
"severityLevel": "8957685N8Y",
}
],
"affectedCounts": {
"INFRA": 1,
"STRUCTURE": 0,
"APPLICATION": 0,
},
"recoveredCounts": {
"INFRA": 0,
"STRUCTURE": 0,
"APPLICATION": 0,
},
"RootCause": true
}
]
}
}
下面是我正在使用的代码:
Get-Content C:\Documents\feed.json -Raw | ConvertFrom-Json | Select -Expand result | Select -Expand problems | Select * -Expand tagsOfEntities | Select * -Expand ranked | ConvertTo-Csv -NoTypeInformation | Out-File C:\Documents\output.csv
上面的代码给出了正确的输出csv文件,但是它运行了2个小时。我在做错什么吗?
这是output.csv fil中的输出。所有这些数据都放在一个单元格中。
CONTEXTLESS,“ 1”,“应用程序”,“ 843675746378564876”,“ 843675746378564876”,“ 8957685N8Y”,“ AHR157689”,“ YOCETJE”,“结构”,“ OPEN”,“ @ {” INFRA“:1; “ STRUCTURE”:0,“ APPLICATION”:0}“,” @ {“ INFRA”:0;“ STRUCTURE”:0;“ APPLICATION”:0}“,” true“
答案 0 :(得分:0)
您输入的JSON有两个问题。
此外,Powershell正在选择results
,但是JSON具有result
。同样,它选择problem
,但是JSON具有problems
。
以下是纠正JSON和其他一些问题的示例:
@"
{
"result": {
"problems": [
{
"id": "AHR157689",
"displayName": "YOCETJE",
"impact": "STRUCTURE",
"status": "OPEN",
"tagsOfEntities": [],
"ranked": [
{
"entityId": "843675746378564876",
"entityName": "HGFUTGYJDH",
"severityLevel": "8957685N8Y"
}
],
"affectedCounts": {
"INFRA": 1,
"STRUCTURE": 0,
"APPLICATION": 0
},
"recoveredCounts": {
"INFRA": 0,
"STRUCTURE": 0,
"APPLICATION": 0
},
"RootCause": true
}
]
}
}
"@ | ConvertFrom-Json | Select -Expand result | Select -Expand problems |
Select * -Expand ranked | ConvertTo-Csv -NoTypeInformation | Out-File .\output.csv