You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell转换复杂嵌套JSON为CSV时无法遍历所有产品数据

PowerShell 嵌套JSON批量转CSV实现方案

现存脚本问题

两个版本的测试脚本均存在逻辑缺陷,无法达成预期效果:

  • 初始版本硬编码读取PRD_GV_BCA单个产品节点,无法自动遍历全量约40个产品(如PRD_GV_BEU等)的子条目,也无法正确将产品名称赋值给Produkt输出字段
  • 调整后版本虽能获取产品名称,但因错误将结构化对象转为字符串处理,无法正确读取子项的iconCode、labelKey、descriptionKey、gedeckt属性,且仅能输出单个产品的数据

初始缺陷脚本

$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

调整后缺陷脚本

$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

输入JSON样例结构

{
    "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
        }
    ]
}

可用实现脚本

核心逻辑是通过PSObject.Properties遍历根节点下所有产品属性,无需硬编码产品名即可自动适配全量产品节点:

# 替换为实际文件路径
$pathToJsonFile = "<Pfad zum Skript>\jsontocsv\tjson.json"
$pathToOutputFile = "<Pfad zur Ausgabedatei>\jsontocsv\converts.csv"

# 一次性读取并解析JSON,避免分段读取解析异常
$jsonData = Get-Content -Path $pathToJsonFile -Raw | ConvertFrom-Json
$result = @()

# 遍历所有产品节点
$jsonData.PSObject.Properties | ForEach-Object {
    $currentProduct = $_.Name
    # 遍历当前产品下的所有子条目,组装导出行
    $_.Value | ForEach-Object {
        $result += [pscustomobject]@{
            Produkt        = $currentProduct
            iconCode       = $_.iconCode
            labelKey       = $_.labelKey
            descriptionKey = $_.descriptionKey
            gedeckt        = $_.gedeckt
        }
    }
}

# 导出为UTF8编码CSV,避免特殊字符乱码
$result | Export-Csv -Path $pathToOutputFile -NoTypeInformation -Encoding UTF8

关键说明

  • 脚本自动识别所有产品节点,后续新增/删除产品不需要修改代码
  • 读取JSON时加-Raw参数,确保完整加载JSON结构,避免解析错误
  • 导出指定UTF8编码,适配德语法语等特殊字符显示
  • 输出格式完全匹配预期:每行对应一个产品下的单条保障记录,第一列为产品名称,后续列对应子项属性

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 10:18:07