How to change tab width when converting to JSON in Powershell

前端 未结 4 1143
执笔经年
执笔经年 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 13:17

    The following code will halve the size of indent:

    $json = @"
    {
        "Phone":  "SomePhone",
        "Description":  "Lorem ipsum dolor..",
        "Price":  99.99
    }
    "@
    
    ($json -split '\r\n' |
    % {
      $line = $_
      if ($_ -match '^ +') {
        $len  = $Matches[0].Length / 2
        $line = ' ' * $len + $line.TrimStart()
      }
      $line
    }) -join "`r`n"
    

提交回复
热议问题