How to change tab width when converting to JSON in Powershell

前端 未结 4 1149
执笔经年
执笔经年 2021-02-05 12:21

I am creating a JSON in Powershell and I want to set a custom tab width when building it (instead of the default 4 white spaces I want to set only 2 white spaces).

I am

4条回答
  •  花落未央
    2021-02-05 12:55

    Here is a simple method:

    $data_json | convertto-json -depth 100 |
        foreach-object {$_ -replace "(?m)  (?<=^(?:  )*)", "`t" } |
        set-content 'output.json'
    

    The foreach-object catches if you pass more than one object to ConvertTo-JSON.

    Change the "`t" to what ever you want to do with the indent.

提交回复
热议问题