BorgRebel
BorgRebel

Reputation: 518

What is a better way to remove child members of a PowerShell JSON Object

I have a working loop in PowerShell which will remove child members from a JSON object if they exist. However, I believe there's a cleaner way to do this. Do you know how?

for ($i = 0; $i -lt $destinationReleaseDefinitionJSON.environments.Count; $i++) {
    $destinationReleaseDefinitionJSON.environments[$i] = $destinationReleaseDefinitionJSON.environments[$i] | Select-Object * -ExcludeProperty queueId
    for ($ii = 0; $ii -lt $destinationReleaseDefinitionJSON.environments[$i].deployPhases.Count; $ii++) {
        $destinationReleaseDefinitionJSON.environments[$i].deployPhases[$ii].deploymentInput = $destinationReleaseDefinitionJSON.environments[$i].deployPhases[$ii].deploymentInput | Select-Object * -ExcludeProperty queueId
    }
}

Upvotes: 3

Views: 1938

Answers (1)

woxxom
woxxom

Reputation: 73556

Assuming you want to delete just one property, do it directly via .PSObject.Properties.Remove:

foreach ($env in $destinationReleaseDefinitionJSON.environments) {
    $env.PSObject.Properties.Remove('queueId')
    foreach ($phase in $env.deployPhases) {
        $phase.deploymentInput.PSObject.Properties.Remove('queueId')
    }
}

Upvotes: 5

Related Questions