Prettify json in powershell 3

后端 未结 5 1418
清酒与你
清酒与你 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:12

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

提交回复
热议问题