Home > Net >  Extract value from json using powershell
Extract value from json using powershell

Time:05-26

I am having the below json

{
    "clusterName": "IBDCluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "X92SL224XXX2XX:3306", 
        "ssl": "REQUIRED", 
        "status": "OK_NO_TOLERANCE", 
        "statusText": "Cluster is NOT tolerant to any failures. 1 member is not active.", 
        "topology": {
            "X92SL224XXX1XX:3306": {
                "address": "X92SL224XXXXXXX:3306", 
                "memberRole": "SECONDARY", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "5.7.36"
            }, 
            "X92SL224XXX2XX:3306": {
                "address": "X92SL224XXX2XX:3306", 
                "memberRole": "PRIMARY", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "5.7.36"
            }, 
            "X92SL224XXXX3XX:3306": {
                "address": "X92SL224XXX3XX:3306", 
                "instanceErrors": [
                    "ERROR: group_replication has stopped with an error."
                ], 
                "memberRole": "SECONDARY", 
                "memberState": "ERROR", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "role": "HA", 
                "status": "(MISSING)", 
                "version": "5.7.36"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "X92SL224XXXXXXX:3306"
}

I need to extract value like memberRole, status from the topology section.

when I go to the topology part

$ClusterDetails = $ClusterStatus.defaultReplicaSet.topology

the $ClusterDetails have value like (data visible only for 2 servers but all 3 servers are present)

PS C:\Windows\system32> $ClusterDetails

X92SL224XXXX1XX:3306                                                                                                   X92SL224XXXX2XX:3306                              
--------------------                                                                                                   --------------------                              
@{address=X92SL224XXXX1XX:3306; memberRole=SECONDARY; mode=R/O; readReplicas=; role=HA; status=ONLINE; version=5.7.36} @{address=X92SL224XXXX2XX:3306; memberRole=PRIM...

from shell I am able to see the individual output if i select like

PS C:\Windows\system32> $ClusterDetails.'X92SL224XXXX1XX:3306'


address      : X92SL224XXXX1XX:3306
memberRole   : PRIMARY
mode         : R/W
readReplicas : 
role         : HA
status       : ONLINE
version      : 5.7.36

I need help to fetch the data from $ClusterDetails for individual servers like above but not getting how to get that dot part via script. please let me know how to do that.

CodePudding user response:

Quite a long statement but this should work:

$json.defaultReplicaSet.topology.PSObject.Properties.Value | Select-Object memberRole, status

# Results in:

memberRole status
---------- ------
SECONDARY  ONLINE
PRIMARY    ONLINE
SECONDARY  (MISSING)

You can access the Values of each Property of the Object in $json.defaultReplicaSet.topology accessing the PSObject Properties.

It's worth noting that .PSObject.Properties.Value works to enumerate all Property Values at once due to Member-Access Enumeration.

The same can be accomplished using a loop, for example:

foreach($property in $json.defaultReplicaSet.topology.PSObject.Properties) {
    [pscustomobject]@{
        ThisProperty = $property.Name
        memberRole   = $property.Value.memberRole
        status       = $property.Value.status
    }
}
  • Related