<- all articles

ConvertTo-Json depth is a quiet source of broken automation

Nested PowerShell objects can serialize into incomplete JSON without looking obviously broken. Make depth an intentional part of the contract.

PowerShell makes JSON serialization easy enough that it is tempting to treat it as mechanical. Nested objects are where that assumption starts costing time.

The failure shape

When the object graph is deeper than the serializer’s selected depth, nested values can be reduced instead of represented as the structure an API expects.

$payload = [ordered]@{
    device = @{
        settings = @{
            updateRing = @{
                channel = "stable"
            }
        }
    }
}

$json = $payload | ConvertTo-Json -Depth 6

The exact number is less important than making it deliberate. Pick a depth that covers the schema, then assert the serialized shape in a test.

Treat JSON as an output contract

Do not only inspect the PowerShell object. Capture the final JSON string, parse it again, and verify required nested properties. This catches depth mistakes before an API returns an unhelpful validation error.

This page is AI-generated and may contain mistakes. Check linked sources and vendor documentation before making production changes.