将嵌套 JSON 输出导出到 CSV 文件

问题描述 投票:0回答:1

我正在尝试找到一种将嵌套 JSON 数据导出到 CSV 文件中的列中的方法。

这是我们在第 3 方解决方案中进行的一项检查的 JSON 输出。

{
        "url": "***",
        "id": 46092,
        "guid": "a200efc4-2b05-422a-8785-ca5868aa7c1d",
        "name": "***",
        "check_type": "FprXnet",
        "check_type_name": "Real Browser, Chrome",
        "check_type_api": "browser",
        "enabled": true,
        "location": "Finland, Helsinki",
        "country_code": "FI",
        "sla_percent_current_month": 99.44116132753345,
        "timestamp_utc": "2023-07-31T13:45:03.563",
        "severity": "I",
        "value": 37106,
        "unit": "ms",
        "target_sla": null,
        "check_symbol": "N7_M13522_C46092_FPR_20190619_141926_713",
        "threshold_w": null,
        "threshold_w_dynamic": null,
        "threshold_e": null,
        "threshold_e_dynamic": null,
        "threshold_lo_w": null,
        "threshold_lo_w_dynamic": null,
        "threshold_lo_e": null,
        "threshold_lo_e_dynamic": null,
        "scheduled_inclusion": null,
        "scheduled_exclusion": "mon-sun : 01:00-01:15;",
        "interval_seconds": 600,
        "last_result_details": {
            "message": "10 steps, 10 pages, 296 urls, 185350/46334226 sent/received bytes",
            "attempts": 1,
            "result_code": 0
        },
        "tags": {
            "24/7 procedure": [
                "24/7"
            ],
            "Country": [
                "Finland"
            ],
            "Environment": [
                "Prod"
            ],
            "ITSystemCode": [
                "***"
            ]
        }
    },

这就是 CSV 的导出方式:

Exported CSV file example

我需要做的是添加额外的列:

(“24/7 程序”、“国家”、“环境”和“IT 系统代码”)

在 CSV 文件中包含以下嵌套信息。

"tags": {
            "24/7 procedure": [
                "24/7"
            ],
            "Country": [
                "Finland"
            ],
            "Environment": [
                "Prod"
            ],
            "ITSystemCode": [
                "***"
            ]
        }

这是我到目前为止得到的当前脚本:

$response = Invoke-RestMethod 'https://***.***.com/v3/checks?enabled=true&auth_ticket=***' -Method 'GET'

$date = Get-Date -Format "MM-dd-yyyy-HH-mm"

$response | Select-Object -Property name,location,id,tags,timestamp_utc | Export-Csv -Path "Checks_$date.csv" -NoTypeInformation -Delimiter ";"

我正在尝试导出 CSV 文件,因此它包含如下列:

名称,位置,id,标签(“24/7程序”,“国家”,“环境”,“IT系统代码”),timestamp_utc

尝试遵循这一点,但我无法让它工作/并不真正理解这一点,因为我目前正在从 Python 过渡到 PowerShell。

powershell export-to-csv
1个回答
0
投票

这将接近您正在寻找的内容,基本上您创建了一个可选属性的数组,并在其中包含计算属性,用于

.tags
中的嵌套属性:

$properties = @(
    'name'
    'location'
    'id'
    @{ N = '24/7 procedure'; E = { $_.tags.'24/7 procedure' } }
    @{ N = 'Country'; E = { $_.tags.Country } }
    @{ N = 'Environment'; E = { $_.tags.Environment } }
    @{ N = 'ITSystemCode'; E = { $_.tags.ITSystemCode } }
    'timestamp_utc'
)

$date = Get-Date -Format 'MM-dd-yyyy-HH-mm'
$response | Select-Object $properties |
    Export-Csv "Checks_$date.csv" -NoTypeInformation -Delimiter ';'

此示例生成的 Csv 如下所示:

"name";"location";"id";"24/7 procedure";"Country";"Environment";"ITSystemCode";"timestamp_utc"
"***";"Finland, Helsinki";"46092";"24/7";"Finland";"Prod";"***";"7/31/2023 1:45:03 PM"

如果带有

计算属性
Select-Object 语句对您来说太混乱了,您可能会发现通过创建
$response
的新对象输出会更容易,为此请参阅 您想了解的有关 PSCustomObject 的所有内容 .

$response | ForEach-Object {
    [pscustomobject]@{
        'name'           = $_.name
        'location'       = $_.location
        'id'             = $_.id
        '24/7 procedure' = $_.tags.'24/7 procedure' 
        'Country'        = $_.tags.Country
        'Environment'    = $_.tags.Environment
        'ITSystemCode'   = $_.tags.ITSystemCode
        'timestamp_utc'  = $_.timestamp_utc
    }
} | Export-Csv "Checks_$date.csv" -NoTypeInformation -Delimiter ';'
© www.soinside.com 2019 - 2024. All rights reserved.