functions/Format-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
36
37
38
39
40
41
42
43
44
45
# Original code obtained from https://github.com/PowerShell/PowerShell/issues/2736

# Formats JSON in a nicer format than the built-in ConvertTo-Json does.
function Format-Json {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory, ValueFromPipeline)]
        [string]
        $InputObject
    )
    Begin {
        $Buffer = New-Object 'System.Collections.Generic.List[string]'
    }

    Process {
        $Buffer.Add($InputObject)
    }

    End {
        $json = [string]::Join("`n", $Buffer.ToArray())

        [int]$indent = 0;
        $result = ($json -Split '\n' |
            % {
                if ($_ -match '^\s*[\}\]]') {
                    # This line contains ] or }, decrement the indentation level
                    if (--$indent -lt 0) {
                        #fail safe
                        $indent = 0
                    }
                }
                $line = (' ' * $indent * 2) + $_.TrimStart().Replace(': ', ': ')
                if ($_ -match '[\[\{](?!(.*[\{\[\"]))') {
                    # This line contains [ or {, increment the indentation level
                    $indent++
                }
                $line
            }) -Join "`n"

        # Unescape Html characters (<>&')
        $result.Replace('\u0027', "'").Replace('\u003c', "<").Replace('\u003e', ">").Replace('\u0026', "&")
    }
}