Merge two json objects

Join (Join-Object) is not a built-in CmdLet

This is an extension of @Mark’s answer which also recurses through child objects.

function merge ($target, $source) {
    $source.psobject.Properties | % {
        if ($_.TypeNameOfValue -eq 'System.Management.Automation.PSCustomObject' -and $target."$($_.Name)" ) {
            merge $target."$($_.Name)" $_.Value
        }
        else {
            $target | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value -Force
        }
    }
}

merge $Json1 $Json2

$Json1

Leave a Comment