powershell json到CSV复杂的JSON文件

发布于 2025-02-13 05:22:32 字数 3059 浏览 1 评论 0原文

我正在尝试将复杂的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

Query的结果显示了产品的显示

感谢您的帮助/支持 问候 克里斯

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2025-02-20 05:22:32

通过使用out-string,您将获得一个多行字符串,该字符串不再是一组带有数据的对象。

为此,您需要对对象属性进行更多循环,例如:

$pathToJsonFile   = "<Pfad zum Skript>\jsontocsv\tjson.json"
$pathToOutputFile = "<Pfad zum Skript>\jsontocsv\converts.csv"

$deckung = Get-Content -Path $pathToJsonFile -Raw | ConvertFrom-Json | ForEach-Object {
    foreach ($item in $_ ) {
        foreach ($produkt in $item.PsObject.Properties) {
            foreach ($detail in $produkt.Value) {
                [PsCustomObject] @{
                    Produkt        = $produkt.Name
                    iconCode       = $detail.iconCode
                    labelKey       = $detail.labelKey
                    descriptionKey = $detail.descriptionKey
                    gedeckt        = $detail.gedeckt
                }
            }
        }
    }
}

# output to CSV file
$deckung | Export-Csv $pathToOutputFile -NoTypeInformation -UseCulture

注意:通过将开关- 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:

$pathToJsonFile   = "<Pfad zum Skript>\jsontocsv\tjson.json"
$pathToOutputFile = "<Pfad zum Skript>\jsontocsv\converts.csv"

$deckung = Get-Content -Path $pathToJsonFile -Raw | ConvertFrom-Json | ForEach-Object {
    foreach ($item in $_ ) {
        foreach ($produkt in $item.PsObject.Properties) {
            foreach ($detail in $produkt.Value) {
                [PsCustomObject] @{
                    Produkt        = $produkt.Name
                    iconCode       = $detail.iconCode
                    labelKey       = $detail.labelKey
                    descriptionKey = $detail.descriptionKey
                    gedeckt        = $detail.gedeckt
                }
            }
        }
    }
}

# output to CSV file
$deckung | Export-Csv $pathToOutputFile -NoTypeInformation -UseCulture

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:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文