powershell json到CSV复杂的JSON文件
我正在尝试将复杂的JSON文件转换为CSV文件。我现在尝试了几件事,此查询效果最好,不幸的是我无法显示该产品 要显示的产品将是JSON文件“ PRD_GV_BCA” 中的以下内容。 不幸的是,我只从一种产品而不是从另一种产品中显示出其他值的其他值。 (当然,也可以在下面的代码中输入产品,不知道我如何在那里使用占位符或变化,我对PowerShell还不是很好)
您有一种方法
希望 脚本结果 结果
$pathToJsonFile = "<Pfad zum Skript>\jsontocsv\tjson.json"
$pathToOutputFile = "<pfad zur Excel Datei>\jsontocsv\converts.csv"
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json) | ForEach-Object {
$Produkt = $_
$deckung += $_.PRD_GV_BCA | ForEach-Object {
[pscustomobject] @{
'Produkt' = $Produkt
'iconCode' = $_.iconCode
'labelKey' = $_.labelKey
'descriptionKey' = $_.descriptionKey
'gedeckt' = $_.gedeckt
}
}
write-host $deckung
}
$deckung | Export-CSV $pathToOutputFile -NoTypeInformation
这是输入文件中的一部分,还有更多条目(Roundabout 40产品和子条目
{
"PRD_GV_BCA": [{
"iconCode": "custom:KVG.ArztwahlManagedCare",
"labelKey": "ArztwahlManagedCare.label",
"descriptionKey": "KVG.ArztwahlManagedCare.description",
"gedeckt": true
},
{
"iconCode": "custom:KVG.Franchise",
"labelKey": "Franchise.label",
"descriptionKey": "KVG.Franchise.description",
"gedeckt": true
}, {
"iconCode": "custom:KVG.VVG.Versichertenkarte",
"labelKey": "VersichertenkarteKVG.label",
"descriptionKey": "KVG.VersichertenkarteKVG.description",
"gedeckt": true
}
],
"PRD_GV_BEU": [{
"iconCode": "custom:EGK-KVG_EU",
"labelKey": "EU.label",
"descriptionKey": "KVG.EU.description",
"gedeckt": true
},
{
"iconCode": "custom:KVG.VVG.Versichertenkarte",
"labelKey": "VersichertenkarteKVG.label",
"descriptionKey": "KVG.VersichertenkarteKVG.description",
"gedeckt": true
}
]
}
更新: 有了这个查询,我会得到该产品,但没有其他产品。但也只有一种产品
$pathToJsonFile = "<Pfad zum Skript>\jsontocsv\tjson.json"
$pathToOutputFile = "<Pfad zum Skript>\jsontocsv\converts.csv"
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json) | ForEach-Object {
$test = ($_ |out-string).trim()
$test2 = $test | ForEach-Object {
[pscustomobject] @{
'Produkt' = $test
'iconCode' = $_.iconCode
'labelKey' = $_.labelKey
'descriptionKey' = $_.descriptionKey
'gedeckt' = $_.gedeckt
}
}
}
$test2 | Export-CSV $pathToOutputFile -NoTypeInformation
感谢您的帮助/支持 问候 克里斯
I'm trying to convert a complex json file into a csv file. I've tried a few things now and this query worked best, unfortunately I can't get the product to be displayed
The product to be displayed would be the following in the json file "PRD_GV_BCA"
Unfortunately, I only get the other values displayed with the query from one product and not from another. (Of course, also enter a product in the code below, don't know how I can use a placeholder there or that changes, I'm not that good with Powershell yet)
Hope you have an approach how I can do it differently
current result from Script
current result
that how it should looks like
that how it should look like
$pathToJsonFile = "<Pfad zum Skript>\jsontocsv\tjson.json"
$pathToOutputFile = "<pfad zur Excel Datei>\jsontocsv\converts.csv"
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json) | ForEach-Object {
$Produkt = $_
$deckung += $_.PRD_GV_BCA | ForEach-Object {
[pscustomobject] @{
'Produkt' = $Produkt
'iconCode' = $_.iconCode
'labelKey' = $_.labelKey
'descriptionKey' = $_.descriptionKey
'gedeckt' = $_.gedeckt
}
}
write-host $deckung
}
$deckung | Export-CSV $pathToOutputFile -NoTypeInformation
thats a part from the Input File, there more entries (roundabout 40 products and there sub entries
{
"PRD_GV_BCA": [{
"iconCode": "custom:KVG.ArztwahlManagedCare",
"labelKey": "ArztwahlManagedCare.label",
"descriptionKey": "KVG.ArztwahlManagedCare.description",
"gedeckt": true
},
{
"iconCode": "custom:KVG.Franchise",
"labelKey": "Franchise.label",
"descriptionKey": "KVG.Franchise.description",
"gedeckt": true
}, {
"iconCode": "custom:KVG.VVG.Versichertenkarte",
"labelKey": "VersichertenkarteKVG.label",
"descriptionKey": "KVG.VersichertenkarteKVG.description",
"gedeckt": true
}
],
"PRD_GV_BEU": [{
"iconCode": "custom:EGK-KVG_EU",
"labelKey": "EU.label",
"descriptionKey": "KVG.EU.description",
"gedeckt": true
},
{
"iconCode": "custom:KVG.VVG.Versichertenkarte",
"labelKey": "VersichertenkarteKVG.label",
"descriptionKey": "KVG.VersichertenkarteKVG.description",
"gedeckt": true
}
]
}
Update:
with that Query i get the Product but not the rest. But also only one product
$pathToJsonFile = "<Pfad zum Skript>\jsontocsv\tjson.json"
$pathToOutputFile = "<Pfad zum Skript>\jsontocsv\converts.csv"
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json) | ForEach-Object {
$test = ($_ |out-string).trim()
$test2 = $test | ForEach-Object {
[pscustomobject] @{
'Produkt' = $test
'iconCode' = $_.iconCode
'labelKey' = $_.labelKey
'descriptionKey' = $_.descriptionKey
'gedeckt' = $_.gedeckt
}
}
}
$test2 | Export-CSV $pathToOutputFile -NoTypeInformation
result of the query where product is shown
Thanks for your help / support
regards
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
out-string
,您将获得一个多行字符串,该字符串不再是一组带有数据的对象。为此,您需要对对象属性进行更多循环,例如:
注意:通过将开关
- useculture
附加到导出csv cmdlet,它将使用Excel期望的分界符角色创建CSV ,因此您只需双击文件即可获得此结果:By using
Out-String
, you get a multiline string which is no longer a set of objects with the data.For this you will need to do some more loops over the objects properties like below:
Note: by appending switch
-UseCulture
to the Export-Csv cmdlet, it will create a csv using the delimiter character your Excel expects, so you can simply double-click the file to get this result: