首页 > 解决方案 > Powershell 从嵌套 JSON 导出数据

问题描述

我需要帮助循环遍历以下 JSON 文件并提取“snapshot_groupSnapshotChildren”“entitySnapshot_properties”下的所有属性。例如,“Test”、“Guacamole”和“Heartbeat on Guacamole”是动态的,深度也可以从一个变化子组到另一个子组。

JSON文件


    {
        "snapshot_groupSnapshotChildren": {
            "Test": {
                "entitySnapshot_properties": {
                    "_dependsCondition": "good",
                    "_nextID": "2",
                    "_name": "Test",
                    "_externalId": "baa97724-9ff8-46ad-b23a-d37d283905d7",
                    "objcategory": "",
                    "_id": "1951498570",
                    "_class": "SubGroup",
                    "_group": "1951498570",
                    "_enabled": "true",
                    "_ownerID": "__SiteScopeRoot__"
                },
                "snapshot_groupSnapshotChildren": {
                    "Guacamole": {
                        "entitySnapshot_properties": {
                            "_dependsCondition": "good",
                            "_nextID": "2",
                            "_name": "Guacamole",
                            "_externalId": "f08b1069-1943-479d-bb83-7668d833fa58",
                            "objcategory": "",
                            "_id": "1951498571",
                            "_class": "SubGroup",
                            "_group": "1951498571",
                            "_enabled": "true",
                            "_ownerID": "1951498570"
                        },
                        "snapshot_groupSnapshotChildren": {},
                        "entitySnapshot_name": "Guacamole",
                        "snapshot_alertSnapshotChildren": {},
                        "entitySnapshot_url": "",
                        "snapshot_monitorSnapshotChildren": {
                            "Heartbeat on Guacamole": {
                                "entitySnapshot_properties": {
                                    "_prioritySelection": "MEASURMENT",
                                    "_prevKeyAttrMap": "-84.-19.0.5.115.114.0.19.106.97.118.97.46.117.116.105.108.46.65.114.114.97.121.76.105.115.116.120.-127.-46.29.-103.-57.97.-99.3.0.1.73.0.4.115.105.122.101.120.112.0.0.0.0.119.4.0.0.0.0.120.",
                                    "_name": "Heartbeat on Guacamole",
                                    "_frequency": "600",
                                    "_externalId": "80c24825-c540-4e9d-8203-df83333e0a55",
                                    "_reportTopology": "true",
                                    "_eventPreferenceId": "CommonEventInstancePreferences_default",
                                    "_ownerID": "1951498571"
                                },
                                "monitor_snapshot_hostName": "<hostname>",
                                "monitor_snapshot_fullyQualifiedTarget": "<hostFQDN>",
                                "entitySnapshot_name": "Heartbeat on Guacamole",
                                "snapshot_alertSnapshotChildren": {},
                                "entitySnapshot_url": ""
                            }
                        }
                    }
                },
                "entitySnapshot_name": "Test",
                "snapshot_alertSnapshotChildren": {},
                "entitySnapshot_url": "",
                "snapshot_monitorSnapshotChildren": {}
            }
        },
        "entitySnapshot_name": "SiteScopeRoot",
        "snapshot_alertSnapshotChildren": {},
        "snapshot_preferenceSnapShot": {},
        "entitySnapshot_url": "",
        "snapshot_monitorSnapshotChildren": {}
    }

这是我用于循环的草稿代码,它返回一个错误,即“snapshot_groupSnapshotChildren”不存在。

    $info = Get-Content -Path .\input.json

    If($info -eq $null){
        Write-Host "Unhandled Exception parsing data for $sitescopeFQDNhost`nExit Script" -ForegroundColor Red -BackgroundColor Black
        Exit
    }
    else{
        $ResJsonObj = $info | ConvertFrom-Json #-Depth 99
        foreach ($t in $ResJsonObj.PSObject.Properties)
            {
                Write-Host $t.name
                Write-Host $t.value
            }
    }

让我知道如何遍历嵌套的 json 文件以去除每个“entitySnapshot_properties”下的属性值,我很想获取以下属性值

  1. 测试 -->_name, _class 2.Guacamole -->_name,_ownerid
  2. 鳄梨酱上的心跳 --> _name,monitor_snapshot_hostName

标签: jsonpowershellnestednested-loopsconvertfrom-json

解决方案


推荐阅读