Public/JSON.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#requires -Version 3.0


function Read-JsonFile {
    <#
            .SYNOPSIS
            Reads the contents of a JSON file and removes comments.
            .DESCRIPTION
            Reads the contents of a JSON file and removes comments.
            .PARAMETER Path
            Path of the JSON file to read.
            .EXAMPLE
            Read-JsonFile -Path 'C:\myFile.json'
    #>

    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,Position=1)] [string]$Path
    )

    process {
        $file = Get-Item $path
        [string]$plaintext = $file | Get-Content -Raw
        
        $blockComments = '\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*\/'
        $lineComments = "[^:]//[^\n\r]*[\n\r]?" 

        $cleantext = [regex]::Replace($plaintext.ToString(), "$lineComments|$blockComments", "")

        $json = $cleantext | ConvertFrom-Json

        return $json
    }
}