Search code examples
jsonpowershellrecursionautomationwindows-scripting

Reading the JSON hierarchy (Number of subnodes can be any number) file using Powershell


Reading the JSON hierarchy (Number of subnodes can be any number) file using Powershell. I could do it for one level but need the same for N number of loops and nested loops Need result in Tree view, if not possible then Tabular way with the title on top of the root table

Below is the script so far I have developed:

$Filepath = 'C:\Users\Learner\Test2.JSON'
$JsonContent=gc $Filepath | ConvertFrom-Json
$RootNodeCount=$JsonContent.psobject.Properties.name.Count
$RootNodeName=$JsonContent.psobject.Properties.name
foreach ($Name in $RootNodeName) {Echo $Name-->  $JsonContent.$Name} 

JSON File

{
    "foundation": {
        "network": {
            "resource_group_name": "rg-network-e2",
            "name": "nonprodvnet-e2"
        },
        "diagnostics": {
            "resource_group_name": "rg-mgmt-cu",
            "storage_account_name": "Sreacnt-e2"
        },
        "log_analytics": {
            "resource_group_name": "rg-mgmt-cu",
            "workspace_name": "la-sap-e2"
        },
        "recovery_vault": {
            "resource_group_name": "rg-mgmt-cu",
            "name": "rsv-sap-e2"
        },
        "windows_domain": {
            "domain_name": "dmn.local",
            "ou_path": "CN=Computers,DC=example,DC=com",
            "domain_user": "svc_domainjoin@dmn.local",
            "domain_password": "Pwd01"
        }
    },
    "control_flags": {
        "enable_boot_diagnostics": true,
        "enable_oms": false,
        "enable_backup": true,
        "windows_domain_join": false
    },
    "deployment": {
        "resource_group": {
            "name": "rg-sap-501-e2",
            "location": "eastus2"
        },
        "tags": {
            "owner": "for SAP"
        },
        "os_account": {
            "admin_username": "locadm"
        },
        "proximity_placement_groups": {
            "ppg-501": {}
        },
        "availability_sets": {
            "avset-cs-501": {
                "ppg_name": "ppg-501"
            },
            "avset-app-501": {
                "ppg_name": "ppg-501"
            },
            "avset-db-501": {
                "ppg_name": "ppg-501"
            }
        },
        "load_balancers": {
            "ilb-sap-501": {
                "dv2ascs": {
                    "ip_address": "192.0.18.20",
                    "probe_port": 62000,
                    "subnet": "app"
                },
                "dv2-ers": {
                    "ip_address": "192.0.18.21",
                    "probe_port": 62102,
                    "subnet": "app"
                }
            },
            "ilb-db-501": {
                "dv2-hana": {
                    "ip_address": "192.0.17.12",
                    "probe_port": 62503,
                    "subnet": "db"
                }
            }
        },
        "server_groups": {
            "ascs": {
                "os_type": "linux",
                "sku": "Standard_E4s_v3",
                "availability_set": "avset-cs-501",
                "backup_policy": "sap",
                "ppg_name": "ppg-501",
                "lb_refs": [
                    "dv2-ascs",
                    "dv2-ers"
                ],
                "os_disk_size": "30",
                "enable_accelerated_networking": true,
                "hosts": {
                    "vcs501-01": {
                        "nics": [
                            [
                                "192.0.18.18"
                            ]
                        ]
                    },
                    "vcs501-02": {
                        "nics": [
                            [
                                "192.0.18.19"
                            ]
                        ]
                    }
                },
                "subnet": "app",
                "image_details": {
                    "resource_id": null,
                    "marketplace_reference": {
                        "publisher" : "SUSE",
                        "offer"     : "sles-sap-15-sp1",
                        "sku"       : "gen1",
                        "version": "latest"
                    }
                },
                "disks": [
                    {
                        "name": "usrsap",
                        "disk_size": "128",
                        "number_of_disks": 1
                    }
                ]
            },
            "apps": {
                "type": "app_linux",
                "os_type": "linux",
                "sku": "Standard_E4s_v3",
                "availability_set": "avset-app-501",
                "backup_policy": "sap",
                "ppg_name": "ppg-501",
                "enable_accelerated_networking": true,
                "os_disk_size": "30",
                "hosts": {
                    "vas501-01": {
                        "nics": [
                            [
                                "192.0.18.16",
                                "192.0.18.26"
                            ]
                        ]
                    },
                    "vas501-02": {
                        "nics": [
                            [
                                "192.0.18.17",
                                "192.0.18.27"
                            ]
                        ]
                    }
                },
                "subnet": "app",
                "image_details": {
                    "resource_id": null,
                    "marketplace_reference": {
                        "publisher" : "SUSE",
                        "offer"     : "sles-15-sp1",
                        "sku"       : "gen1",
                        "version": "latest"
                    }
                },
                "disks": [
                    {
                        "name": "usrsap",
                        "disk_size": "128",
                        "number_of_disks": 1
                    }
                ]
            },
            "hana": {
                "os_type": "linux",
                "sku": "Standard_E32s_v3",
                "availability_set": "avset-db-501",
                "backup_policy": "sap",
                "ppg_name": "ppg-501",
                "os_disk_size": "30",
                "subnet": "db",
                "lb_refs": [
                    "dv2-hana"
                ],
                "hosts": {
                    "vhs501-01": {
                        "nics": [
                            [
                                "192.0.17.10"
                            ]
                        ]
                    },
                    "vhs501-02": {
                        "nics": [
                            [
                                "192.0.17.11"
                            ]
                        ]
                    }
                },
                "image_details": {
                    "resource_id": null,
                    "marketplace_reference": {
                        "publisher" : "SUSE",
                        "offer"     : "sles-sap-15-sp1",
                        "sku"       : "gen1",
                        "version": "latest"
                    }
                },
                "disks": [
                    {
                        "name": "usrsap",
                        "disk_size": "128",
                        "number_of_disks": 1
                    },
                    {
                        "name": "hanashared",
                        "disk_size": "128",
                        "number_of_disks": 1
                    },
                    {
                        "name": "hanadata",
                        "disk_size": "128",
                        "number_of_disks": 3
                    },
                    {
                        "name": "hanalog",
                        "disk_size": "128",
                        "number_of_disks": 2
                    },
                    {
                        "name": "hanabackup",
                        "disk_size": "512",
                        "number_of_disks": 1
                    }
                ]
            }
        }
    }
}

Solution

  • You are not really reading a Json file but actually the hierarchy of a PowerShell object which might hold a lot of (.Net) object types but in this case it is limited by the original Json structure which only consists out of three major types:

    To best way to read through a hierarchic structure of an unknown depth is to use a recursive function, meaning a function that calls itself. To give you an example for your specific question:

    Function Show-Object ($Object, $Depth = 0, $Name) {
        $Indent = if ($Depth++) { "    " * ($Depth - 2) }
        if ($Object -is [Array]) {
            If ($Name) { "$Indent$Name =" }
            foreach ($Item in $Object) {
                Show-Object $Item $Depth
            }
        }
        elseif ($Object -is [PSCustomObject]) {
            If ($Name) { "$Indent$Name =" }
            foreach ($Name in $Object.PSObject.Properties.Name) {
                Show-Object $Object.$Name $Depth $Name
            }
        }
        else {
            if ($Name) { "$Indent$Name = $Object" } else { "$Indent$Object" }
        }
    }
    

    As you can see, where the structure might any child objects (in case of an [Array] or an [PSCustomObject]), the Show-Object function is calling itself with a child object as the new input object, and some additional parameters as to e.g. keep track of the current $Depth.

    You can call the function simply like:

    Show-Object $JsonContent
    

    Or export it to a file like:

    Show-Object $JsonContent | Out-File .\text1.txt
    

    For your $JsonContent it will result in:

    foundation =
        network =
            resource_group_name = rg-network-e2
            name = nonprodvnet-e2
        diagnostics =
            resource_group_name = rg-mgmt-cu
            storage_account_name = Sreacnt-e2
        log_analytics =
            resource_group_name = rg-mgmt-cu
            workspace_name = la-sap-e2
        recovery_vault =
            resource_group_name = rg-mgmt-cu
            name = rsv-sap-e2
        windows_domain =
            domain_name = dmn.local
            ou_path = CN=Computers,DC=example,DC=com
            domain_user = svc_domainjoin@dmn.local
            domain_password = Pwd01
    control_flags =
        enable_boot_diagnostics = True
        enable_oms = False
        enable_backup = True
        windows_domain_join = False
    deployment =
        resource_group =
            name = rg-sap-501-e2
            location = eastus2
        tags =
            owner = for SAP
        os_account =
            admin_username = locadm
        proximity_placement_groups =
            ppg-501 =
        availability_sets =
            avset-cs-501 =
                ppg_name = ppg-501
            avset-app-501 =
                ppg_name = ppg-501
            avset-db-501 =
                ppg_name = ppg-501
        load_balancers =
            ilb-sap-501 =
                dv2ascs =
                    ip_address = 192.0.18.20
                    probe_port = 62000
                    subnet = app
                dv2-ers =
                    ip_address = 192.0.18.21
                    probe_port = 62102
                    subnet = app
            ilb-db-501 =
                dv2-hana =
                    ip_address = 192.0.17.12
                    probe_port = 62503
                    subnet = db
        server_groups =
            ascs =
                os_type = linux
                sku = Standard_E4s_v3
                availability_set = avset-cs-501
                backup_policy = sap
                ppg_name = ppg-501
                lb_refs =
                    dv2-ascs
                    dv2-ers
                os_disk_size = 30
                enable_accelerated_networking = True
                hosts =
                    vcs501-01 =
                        nics =
                                192.0.18.18
                    vcs501-02 =
                        nics =
                                192.0.18.19
                subnet = app
                image_details =
                    resource_id =
                    marketplace_reference =
                        publisher = SUSE
                        offer = sles-sap-15-sp1
                        sku = gen1
                        version = latest
                disks =
                        name = usrsap
                        disk_size = 128
                        number_of_disks = 1
            apps =
                type = app_linux
                os_type = linux
                sku = Standard_E4s_v3
                availability_set = avset-app-501
                backup_policy = sap
                ppg_name = ppg-501
                enable_accelerated_networking = True
                os_disk_size = 30
                hosts =
                    vas501-01 =
                        nics =
                                192.0.18.16
                                192.0.18.26
                    vas501-02 =
                        nics =
                                192.0.18.17
                                192.0.18.27
                subnet = app
                image_details =
                    resource_id =
                    marketplace_reference =
                        publisher = SUSE
                        offer = sles-15-sp1
                        sku = gen1
                        version = latest
                disks =
                        name = usrsap
                        disk_size = 128
                        number_of_disks = 1
            hana =
                os_type = linux
                sku = Standard_E32s_v3
                availability_set = avset-db-501
                backup_policy = sap
                ppg_name = ppg-501
                os_disk_size = 30
                subnet = db
                lb_refs =
                    dv2-hana
                hosts =
                    vhs501-01 =
                        nics =
                                192.0.17.10
                    vhs501-02 =
                        nics =
                                192.0.17.11
                image_details =
                    resource_id =
                    marketplace_reference =
                        publisher = SUSE
                        offer = sles-sap-15-sp1
                        sku = gen1
                        version = latest
                disks =
                        name = usrsap
                        disk_size = 128
                        number_of_disks = 1
                        name = hanashared
                        disk_size = 128
                        number_of_disks = 1
                        name = hanadata
                        disk_size = 128
                        number_of_disks = 3
                        name = hanalog
                        disk_size = 128
                        number_of_disks = 2
                        name = hanabackup
                        disk_size = 512
                        number_of_disks = 1