我正在尝试使用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文件中的输出。所有这些数据都在一个单元格中。
CONTEXTLESS,"1",“应用程序”,"843675746378564876","843675746378564876","8957685N8Y","AHR157689","YOCETJE",“结构”,“打开”,“@{”基础结构“:1;”结构“:0,”应用程序“:0}",”@{“基础结构”:0;“结构”:0;“应用程序”:0},“真”
发布于 2019-08-23 06:53:55
您的输入JSON有两个问题。
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
https://stackoverflow.com/questions/57559460
复制相似问题