Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change tab width when converting to JSON in Powershell

I am creating a JSON in Powershell and I want to set a custom tab width when building it (instead of the default 4 white spaces I want to set only 2 white spaces).

I am doing this because:

  • the actual JSON (not the one presented in the below sample) is pretty big (100k+ rows) and if not archieved, it's size is pretty big; If I reduce the tab width the size reduction is notable.
  • the actual JSON has a depth of 5+ nodes !
  • I cannot use -Compress since the JSON needs to be human readable
  • Yes, I agree, if archived, it's size is dramatically reduced but I need it also unarchived.

Sample code:

$object = New-Object PSObject
Add-Member -InputObject $object -MemberType NoteProperty -Name Phone -Value "SomePhone"
Add-Member -InputObject $object -MemberType NoteProperty -Name Description -Value "Lorem ipsum dolor.."
Add-Member -InputObject $object -MemberType NoteProperty -Name Price -Value 99.99

$object | ConvertTo-Json

Result with tab width = 4 white space characters.

{
    "Phone":  "SomePhone",
    "Description":  "Lorem ipsum dolor..",
    "Price":  99.99
}

I tried compression but it doesn't give control over compression level (how agressive compression should be)

$object | ConvertTo-Json -Compress

Result compressed, obviously.

{"Phone":"SomePhone","Description":"Lorem ipsum dolor..","Price":99.99}

What I am trying to achieve: result with tab width = 2 white space characters.

{
  "Phone":  "SomePhone",
  "Description":  "Lorem ipsum dolor..",
  "Price":  99.99
}

What I've tried so far is in the pseudo code below. I'm still in the loop. Please get me out of there :)

while (1) {
    Google, StackOverflow
    Try Stuff found 
    Tweak stuff found

    if (Correct answer) {
        break
    }
}
like image 378
Eduard Uta Avatar asked Oct 15 '15 09:10

Eduard Uta


People also ask

What is JSON Depth?

-Depth. Specifies how many levels of contained objects are included in the JSON representation. The value can be any number from 0 to 100 . The default value is 2 .

How do I parse JSON data in PowerShell?

PowerShell makes it easy to modify JSON by converting JSON to a PSCustomObject. The object can then be modified easily like any other object. The object can then be exported back out using ConvertTo-Json. Now if we're on a computer without PowerShell 7.1 we try to run the same command in PowerShell 5.1 but it fails!

Does PowerShell support JSON?

To generate a JSON string from any object, use the ConvertTo-Json cmdlet. This cmdlet was introduced in PowerShell 3.0. Beginning with PowerShell 6, this cmdlet supports JSON with comments.


1 Answers

Because the PowerShell's ConvertTo-Json produces non-deterministic indentation, the current answers will not produce JSON that has exactly two spaces for each depth in the data structure.

To get each level of nested data indented exactly two spaces more than the enclosing level requires rebuilding the indentation. (For what it's worth, looks like this was fixed in PowerShell 6)

After writing up my own solution, I found an almost identical one on GitHub, from Facebook's Daniel Lo Nigro (Daniel15) here. His is a PowerShell function that can take piped input. (I made the regex matches a bit more specific to reduce the likelihood of unintentional matching data.)

# Formats JSON in a nicer format than the built-in ConvertTo-Json does.
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
    $indent = 0;
    ($json -Split "`n" | % {
        if ($_ -match '[\}\]]\s*,?\s*$') {
            # This line ends with ] or }, decrement the indentation level
            $indent--
        }
        $line = ('  ' * $indent) + $($_.TrimStart() -replace '":  (["{[])', '": $1' -replace ':  ', ': ')
        if ($_ -match '[\{\[]\s*$') {
            # This line ends with [ or {, increment the indentation level
            $indent++
        }
        $line
    }) -Join "`n"
}

Usage: $foo | ConvertTo-Json | Format-Json

like image 107
Nimai Avatar answered Oct 03 '22 16:10

Nimai