Skip to main content

PowerShell function to remove comments from a JSON formatted file. You only need to remove JSON comments if you're running an older version of PowerShell, less than version 6 (usually PowerShell Desktop). Comments are automatically stripped in PowerShell Core (v6+).

function Remove-Comments
{
    param(
        [CmdletBinding()]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [string]
        $Json
    )
    process
    {
        $comments = '\/\*[\s\S]*?\*\/|\/\/.*$'
        $emptyLines = '^(?:[\t ]*(?:\r?\n|\r))+'
        $trailingWs = '[ \t]+$'

        $Json = [Regex]::Replace($Json, $comments, '', 'MultiLine')
        $Json = [Regex]::Replace($Json, $emptyLines, '', 'MultiLine')
        $Json = [Regex]::Replace($Json, $trailingWs, '', 'MultiLine')

        return $Json
    }
}

function Remove-CommentsFromFile
{
    param(
        [CmdletBinding()]
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [string]
        [ValidateScript({ Test-Path $_ })]
        $Path
    )

    $content = Get-Content $Path -Raw | Remove-Comments
    $content | Out-File $Path -Force
}

# Strip comments
Get-Content -Path 'example.json' -Raw | Remove-Comments | ConvertFrom-Json

# Strip comments, convert to JSON object, then back to JSON without comments
Get-Content -Path 'example.json' -Raw | Remove-Comments | ConvertFrom-Json | ConvertTo-Json

# -----------------------------------------------------------------------
# example.json
# -----------------------------------------------------------------------
# {//Comment
#   /** Multine Line Comments */
#   "include": [//Comment
#     { //Comment
#       //Comment
#       "label": "My first deploy target",
#       "server-name": "server.name1",//Comment
#       "root-unc-path": "\\\\server.name\\E$\\www\\public",
#       "local-root-path": "E:\\www\\public",
#       "iis-path": "IIS:\\Sites\\Default Web Site",//Comment
#       "enable": true //Comment
#     }, //Comment
#     //Comment
#     {
#       "label": "My second deploy target",
#       "server-name": "server.name2",
#       /** Multine Line Comments
#        *
#        * Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
#       */
#       "root-unc-path": "\\\\server.name2\\Z$\\www\\public\\sub-directory",
#       "local-root-path": "Z:\\www\\public\\sub-directory",
#       "iis-path": "IIS:\\Sites\\Mysite\\MyApp",
#       "enable": true
#     },//Comment
#     {
#       "label": "I am configured, but not enabled. So I will not be run.",
#       "server-name": "server.name3",
#       "root-unc-path": "\\\\server.name3\\Q$\\www\\public\\broken",
#       "local-root-path": "Q:\\www\\public\\broken",
#       "iis-path": "IIS:\\Sites\\Broken site",
#       "enable": false
#     }
#   ]//Comment
# } //Comment
# //Comment
# //Comment