Prettify json in powershell 3
Works for me. Parentheses make sure get-content is done before piping. Default depth of convertto-json is 2, which is often too low.
function pjson ($jsonfile) { (get-content $jsonfile) | convertfrom-json | convertto-json -depth 100 | set-content $jsonfile}
If you really don't want to go down the simplest route, which is to use inbuilt PowerShell functions | ConvertFrom-Json | ConvertTo-Json
, here is another method, using JSON.net
# http://james.newtonking.com/projects/json-net.aspxAdd-Type -Path "DRIVE:\path\to\Newtonsoft.Json.dll"$jsonString = '{ "baz": "quuz", "cow": [ "moo", "cud" ], "foo": "bar" }'[Newtonsoft.Json.Linq.JObject]::Parse($jsonString).ToString()
I put this in my profile
function PrettyPrintJson { param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] $json ) $json | ConvertFrom-Json | ConvertTo-Json -Depth 100}
Which works with pipes, and can be auto-completed, so it's at least somewhat less typing:
cat .\file.json | PrettyPrintJsoncurl https://api.twitter.com/1.1/statuses/user_timeline.json | PrettyPrintJson