【问题标题】:Replace column value with null in json file using Powershell使用Powershell将json文件中的列值替换为null
【发布时间】:2021-01-19 23:49:22
【问题描述】:

这是我的 json 文件的样子:

{
    "count":  12,
    "name":  "Daily Ticket",
    "columnNames":  [
                        "User",
                        "Channel",
                        "Date",
                        "# of Closed Incidents",
                        "Open",
                        "Response",
                        "Remark",
                        "Closed"
                    ],
    "rows":  [
                    [
                     "abc",
                     "Service Web",
                     "\u00272020-06-13 00:00:00\u0027",
                     "1",
                     "0",
                     "0",
                     "this is a text,please replace with null",
                     "1"
                 ],
                 [
                     "xyz",
                     "Email",
                     "\u00272020-06-13 00:00:00\u0027",
                     "21",
                     "1",
                     "0",
                     "this is a text,please replace with null",
                     "7"
                 ]
             ]
}

我想将 Remark 列中的所有值替换为 null 并使用 powershell 转换为 csv 文件。请帮助实现这一目标。

我希望列名作为标题,行作为在 csv 中用逗号分隔的行。 我的输出 csv 文件应如下所示:

User,Channel,Date,# of Closed Incidents,Open,Response,Remark,Closed
abc,Service Web,\u00272020-06-13 00:00:00\u0027,1,0,0,,1
xyz,Email,\u00272020-06-13 00:00:00\u0027,1,0,0,,1

【问题讨论】:

    标签: json powershell csv


    【解决方案1】:

    将此 json 转换为 CSV 文件并不难。
    只需加载 JSON,将其转换为对象并循环通过属性构建可以保存为 CSV 的新对象数组:

    $json = Get-Content -Path 'D:\Test\DailyTicket.json' -Raw | ConvertFrom-Json
    $headers = $json.columnNames
    
    $result = foreach ($row in $json.rows) {
        # just a precaution to not run into index errors when there are 
        # more items in the array than there are headers or vice-versa
        $items = [math]::Min($row.Count, $headers.Count)
        # create a new empty (ordered) hashtable
        $hash  = [ordered]@{}
        for ($i = 0; $i -lt $items; $i++) {
            # fill the hashtable, except for iten 'Remark'
            $hash[$headers[$i]] = if ($headers[$i] -ne 'Remark') { $row[$i] } else { $null }
        }
        # If you insist on keeping the apostrophe characters in the date field in unicode format `\u0027`
        # $hash['Date'] = $hash['Date'] -replace "'", '\u0027'
    
        # output a PSObject to be collected in array $result
        [PsCustomObject]$hash
    }
    
    # output on screen
    $result | Format-Table -AutoSize
    
    # output to CSV file
    $result | Export-Csv -Path 'D:\Test\DailyTicket.csv' -NoTypeInformation
    

    生成的 CSV 文件:

    "User","Channel","Date","# of Closed Incidents","Open","Response","Remark","Closed"
    "abc","Service Web","'2020-06-13 00:00:00'","1","0","0",,"1"
    "xyz","Email","'2020-06-13 00:00:00'","21","1","0",,"7"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2018-08-22
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多