json - 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”下的属性值,我很想获取以下属性值
- 测试 -->_name, _class 2.Guacamole -->_name,_ownerid
- 鳄梨酱上的心跳 --> _name,monitor_snapshot_hostName
解决方案
推荐阅读
- reactjs - 如何在 React 无状态组件中编写和访问函数
- mysql - 创建触发器以防止插入其他表的条件
- android - 用于数字和电子邮件 ID 的 Android 语音转文本
- mongodb - 使用 $first 删除重复文档
- javascript - jquery $get 上的“jQuery 需要一个带有文档的窗口”
- scala - 从 Spark2 中的 spark 临时表创建表后记录丢失
- r - 重新排序字符类数据框列并绘图
- powershell - PowerShell ISE 登录-AzureRmAccount JavaScript 错误
- python - 在同一个 Qwidget 中显示不同的 Paint 类
- c++ - 在哪里可以找到所有可用的 __attribute__ 选项