防止电源外壳对来自 JSON 的结果进行分组



我正在解析 json 文件,并且该过程正在执行我期望的操作,但有一个例外:当我希望它们按行拆分时,它会返回分组在一起的结果。

我试图从文件中获取错误列表

$json = '{
"building": [
{
"buildingname": "NAPA Auto Parts",
"files": [{
"sheets": [{
"name": "BATTERY",
"results": [{
"filename": "BATTERY - 1679568711.xlsx",
"sku": "1679568711"
}
]
}
],
"name": "2.15.19.xlsx",
"status": "processed",
"fileId": "c586bba6-4382-42c4-9c29-bffc6f7fe0b6"
}, {
"name": "Oct-Nov 2018 11.30.18.xlsx",
"errors": ["Unknown sheet name: TOILET PLUNGER"],
"status": "failed",
"fileId": "afa7c43f-26dc-421c-b2eb-45ad1e899c42"
}
]
},
{
"buildingname": "O''Reily Auto Parts",
"files": [{
"sheets": [{
"name": "ALTERNATOR",
"results": [{
"filename": "ALTERNATOR - 6.3.19 1629453444.xlsx",
"sku": "1629453444"
}
]
}, {
"name": "OIL FILTER",
"results": [{
"filename": "OIL FILTER - 6.3.19 1629453444.xlsx",
"sku": "1629453444"
}
]
}
],
"name": "6.3.19.xlsx",
"status": "processed",
"fileId": "647089fe-9592-4e2b-984f-831c4acd4d9c"
}
]
}
]
}'
$psdata = ConvertFrom-Json -InputObject $json
$psdata.building | Select buildingname, @{Name="errors";E={$_.files | Select -Expand errors}}, @{Name="fileId";E={$_.files | Select -Expand fileId}} | Where-Object {$_.errors -ne $null}

我得到了这些结果

buildingname       errors                             fileId                                                                      
------------       ------                             ------                                                                      
NAPA Auto Parts    Unknown sheet name: TOILET PLUNGER {c586bba6-4382-42c4-9c29-bffc6f7fe0b6, afa7c43f-26dc-421c-b2eb-45ad1e899c42}        

这就是我想要得到的

buildingname       errors                             fileId                                                                      
------------       ------                             ------                                                                      
NAPA Auto Parts    Unknown sheet name: TOILET PLUNGER afa7c43f-26dc-421c-b2eb-45ad1e899c42

如何防止电源外壳将结果分组在一起?

抱歉,不确定您到底要做什么,但我现在看到了。像这样循环怎么样:

$psdata.building | foreach-Object {
foreach ($File in $_.files)
{
[PSCustomObject]@{
BuildingName = $_.BuildingName
Errors = & {if ($File.Errors) {$File.Errors}}
fileId = $File.fileId
}
}
}

最新更新