Search code examples
jsonpowershellminify

Minify JSON with PowerShell?


Is there a way to minify (remove all whitespace in this case) a JSON file to turn this

[
    0.000005,
    0,
    0
],
[
    219.740502,
    0.003449,
    4.177065
],
[
    45.210918,
    0.003365,
    -16.008996
],
[
    344.552785,
    0.030213,
    277.614965
],

to this using PowerShell

[0.000005,0,0],[219.740502,0.003449,4.177065],[45.210918,0.003365,-16.008996],[344.552785,0.030213,277.614965],

I have tried several online "minifiers" however the file contains over 100,000 arrays and basically broke all the online minifiers. Any ideas?


Solution

  • Just for your information when you manipulate PowerShell object and convert them to JSON (ConvertTo-Json) you've got the -compress param :

    New-Object -TypeName PSCustomObject -Property @{Name="Hugot";GivenName="Victor"} | ConvertTo-Json -Compress
    

    gives :

    {"GivenName":"Victor","Name":"Hugot"}