Private/New-BodyString.ps1

function New-BodyString {
    <#
    .SYNOPSIS
        Builds the JSON body of a request.

    .DESCRIPTION
        Compares the fields an endpoint accepts, as defined in Get-GhostAPIData, with the parameters that were
        passed to the calling function. Only parameters that were actually passed end up in the body, so an
        update never touches a field the caller did not mention.

        Ghost wraps a resource in an array named after the resource, such as {"posts":[{...}]}. Pass that name
        as -Wrapper.

    .EXAMPLE
        New-BodyString -BodyKeys $resources.Body -Parameters $PSBoundParameters -Wrapper $resources.Result

        Returns, for example, {"posts":[{"title":"Hello"}]}.
    #>

    [CmdletBinding()]
    [OutputType([string])]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
        'PSUseShouldProcessForStateChangingFunctions', '',
        Justification = 'Builds a string. Nothing is changed on the system or on the site.'
    )]
    param(
        # The Body table of the endpoint: parameter name = name of the field in the body.
        [System.Collections.IDictionary] $BodyKeys = @{},

        # The parameters of the calling function, usually $PSBoundParameters.
        [System.Collections.IDictionary] $Parameters = @{},

        # Name of the array that wraps the resource, such as posts.
        [string] $Wrapper
    )

    $fields = [ordered]@{}
    foreach ($name in $BodyKeys.Keys | Sort-Object) {
        if ($Parameters.Keys -contains $name) {
            $fields[$BodyKeys[$name]] = ConvertTo-ApiValue -Value $Parameters[$name]
        }
    }

    if ($fields.Count -eq 0) {
        return $null
    }

    $body = $fields
    if ($Wrapper) {
        $body = @{ $Wrapper = @($fields) }
    }

    ConvertTo-Json -InputObject $body -Depth 20 -Compress
}