Minify JSON with PowerShell?

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"}

You can easily do so with a basic regex. If you have this in a file try the following. You must include the -Raw parameter or the file will be passed one line at a time which will prevent the regex from removing the newline character.

(Get-Content C:\Some\File.json -Raw) -replace '\s','' | out-file C:\some\outfile.json

Adding to JP's answer,

(ConvertFrom-Json $json) | ConvertTo-Json -Compress

This is useful if you already have json. If you don't do the ConvertFrom-Json on the front, then it will encode the line breaks with \r\n and the quotes with \".