Prettify json in powershell 3

后端 未结 5 1399
清酒与你
清酒与你 2020-12-03 04:43

Given a standard json string value:

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

How can I get

5条回答
  •  爱一瞬间的悲伤
    2020-12-03 05:08

    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
    }
    

提交回复
热议问题