0

I need help with looping through the following JSON file and pull in all the attributes under "snapshot_groupSnapshotChildren""entitySnapshot_properties" For example the "Test", "Guacamole" and the "Heartbeat on Guacamole" are dynamic and the depth can also vary from one subgroup to another subgroup.

JSON File


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

Here is my draft code for looping through, which returns an error that "snapshot_groupSnapshotChildren" dosent exist.

    $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
            }
    }

Let me know how i can loop through the nested json file to strip out the atributes values under each "entitySnapshot_properties", I a intrested to fetch the following attribute values

  1. Test -->_name, _class 2.Guacamole -->_name,_ownerid
  2. Heartbeat on Guacamole --> _name,monitor_snapshot_hostName
  • To see if I understood the concept, you want to pull from for ex. `Guacamole.entitySnapshot_properties` the attributes: `_dependsCondition`, `_nextID`, `_name` and so on or you want to pull the **Values** of those attributes? – Santiago Squarzon Oct 31 '21 at 19:39
  • 1
    `ResJsonObj.snapshot_groupSnapshotChildren` --> `$ResJsonObj.snapshot_groupSnapshotChildren` – Theo Oct 31 '21 at 19:42
  • I need to pull the following attributes Test -->_name, _class,_ownerid Guacamole -->_name, _class,_ownerid Heartbeat on Guacamole --> _name,_ownerid, monitor_snapshot_hostName, monitor_snapshot_fullyQualifiedTarget – user17206036 Nov 01 '21 at 16:03
  • Below code gets 1st level data, not sure hot to get the following data 1. Test -->_name, _class 2.Guacamole -->_name,_ownerid 3. Heartbeat on Guacamole --> _name,monitor_snapshot_hostName $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 } } – user17206036 Nov 01 '21 at 18:39

0 Answers0