Private/ConvertTo-SCARequestBody.ps1
|
function ConvertTo-SCARequestBody { <# .SYNOPSIS Serializes a request payload to JSON for the CyberArk SaaS APIs. .DESCRIPTION Wraps ConvertTo-Json with the depth and formatting the module needs everywhere it builds a request body, and drops $null-valued keys from a hashtable/PSCustomObject input so that optional fields are omitted rather than sent as JSON null. .PARAMETER InputObject The hashtable or PSCustomObject to serialize. .PARAMETER Depth Serialization depth passed to ConvertTo-Json. Defaults to 10, which comfortably covers the nested policy and access-request payload shapes used by these APIs. .OUTPUTS System.String #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory, Position = 0)] [object]$InputObject, [int]$Depth = 10 ) if ($InputObject -is [System.Collections.IDictionary]) { $clean = @{} foreach ($key in $InputObject.Keys) { if ($null -ne $InputObject[$key]) { $clean[$key] = $InputObject[$key] } } return $clean | ConvertTo-Json -Depth $Depth -Compress } if ($InputObject -is [psobject] -and $InputObject.PSObject.Properties.Count -gt 0) { $clean = [ordered]@{} foreach ($property in $InputObject.PSObject.Properties) { if ($null -ne $property.Value) { $clean[$property.Name] = $property.Value } } return [pscustomobject]$clean | ConvertTo-Json -Depth $Depth -Compress } return $InputObject | ConvertTo-Json -Depth $Depth -Compress } |