public/Import-ArmTemplateJson.ps1

<#
 .Synopsis
 Replaces a function call in a JSON file with the contents of a given file.
 .Description
 It is a specialized version of Devdeer.Tools.Invoke-JsonContentReplacement which assumes that the
 JSON proviced in the remote file represents an ARM definition JSON including one JSON-section named
 `definition`.
 .Parameter File
 The path to the file in which the replacement should happen and which has the $FunctionName defined.
 .Parameter FunctionName
 The name of the function inside the $File (e.g. `[FunctionName('filePath')]`)
 .OUTPUTS
 System.String. Import-ArmTemplateJson returns the original content of the file so that the caller can undo replacement later.
 .Example
 Import-AzdArmTemplateJson -File .\parameters.json -FunctionName getJson
 Insert JSON based on the function `getJson` in the `parameters.json` file. You would use
 `[getJson('sample.json')]` to define the replace position and the source file for the JSON.
#>

Function Import-ArmTemplateJson {
    param (        
        [Parameter(Mandatory = $true)] [string] $File,
        [Parameter(Mandatory = $true)] [string] $FunctionName,
        [bool] $MakeOneLine = $true    
    )
    $ParameterFileContent = Get-Content $File
    $fileName = [regex]::Matches($ParameterFileContent, "$FunctionName\('(.*?)'").captures.groups[1].value
    $jsonContent = (Get-Content $fileName -Raw)    
    if ($MakeOneLine) {
        $jsonContent = $jsonContent.Replace("`n", "").Replace(" ", "")
    }
    $jsonContent = [regex]::Matches($jsonContent, '{"definition":(.*)}{1}$').captures.groups[1].value
    $jsonContent = $jsonContent.Replace('"', '\"')
    $result = [regex]::Replace($ParameterFileContent, "[\[]$FunctionName\('(.*?)'\)[\]]", $jsonContent)
    Set-Content $File -Value $result
    return $oldContent    
}