Prettify json in powershell 3

JsonPowershellPowershell 3.0

Json Problem Overview


Given a standard json string value:

$jsonString = '{ "baz": "quuz", "cow": [ "moo", "cud" ], "foo": "bar" }'

How can I get this to be all pretty with newlines, preferably without brute-force regex?

Simplest method I've found so far is:

$jsonString | ConvertFrom-Json | ConvertTo-Json 

However, that seems kinda silly.

Json Solutions


Solution 1 - Json

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
}

Solution 2 - Json

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.aspx
Add-Type -Path "DRIVE:\path\to\Newtonsoft.Json.dll"

$jsonString = '{ "baz": "quuz", "cow": [ "moo", "cud" ], "foo": "bar" }'
[Newtonsoft.Json.Linq.JObject]::Parse($jsonString).ToString()

Solution 3 - Json

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 | PrettyPrintJson
curl https://api.twitter.com/1.1/statuses/user_timeline.json | PrettyPrintJson

Solution 4 - Json

Adding to @JS2010's answer I added logic to escape out certain characters and clean up my output even further. The parenthesis seems key and -depth is a big one since you can lose details without it, from what I've seen, on depth that goes beyond the default of 5, I believe it is.

function Format-Json ($JSON)
{
    $PrettifiedJSON = ($JSON) | convertfrom-json | convertto-json -depth 100 | ForEach-Object { [System.Text.RegularExpressions.Regex]::Unescape($_) }
    $PrettifiedJSON
}

Solution 5 - Json

I think what you are looking for is this:

$jsonString = @{ 
'baz' = 'quuz'
'cow'= "moo, cud"
'foo'= "bar" 
}
$jsonString|ConvertTo-Json

it produces this output

{
    "baz":  "quuz",
    "cow":  "moo, cud",
    "foo":  "bar"
}

Added note You could also array your cow values to "prettify" it a bit more:

 $jsonString = @{ 
    'baz' = 'quuz'
    'cow'= @("moo"; "cud")
    'foo'= "bar" 
    }

output:

{
    "baz":  "quuz",
    "cow":  [
                "moo",
                "cud"
            ],
    "foo":  "bar"
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErisView Question on Stackoverflow
Solution 1 - Jsonjs2010View Answer on Stackoverflow
Solution 2 - JsonTechSpudView Answer on Stackoverflow
Solution 3 - JsonbobbalubaView Answer on Stackoverflow
Solution 4 - JsonJim Britt - MSFTView Answer on Stackoverflow
Solution 5 - JsonfampopView Answer on Stackoverflow