简体   繁体   English

使用Powershell从json文件导出csv

[英]export csv from json file using powershell

I do have a json file like this: { "skip" : 0, "take" : 100, "rows" : [ { "WG": "1013", "Werkgever": "1013", "Cao": "0000" } ]} 我确实有一个json文件,例如:{“ skip”:0,“ take”:100,“ rows”:[{“ WG”:“ 1013”,“ Werkgever”:“ 1013”,“ Cao”:“ 0000 “}]}

Now I do need to convert this to csv file like this using powerhsell 现在我确实需要使用powerhsell将其转换为csv文件

"WG","Werkgever","Cao"
"1013","1013","0000"

The script is: 脚本是:

Json conversion to CSV Json转换为CSV

Get-Content -Raw $file |
  ConvertFrom-Json |
  select @{n='WG';e={$_.WG | select -Expand WG}},
         @{n='Werkgever';e={$_.Werkgever | select -Expand Werkgever}},
         @{n='Cao';e={$_.Cao| select -Expand Cao}}|
Export-Csv $FileName1 -NoType

Only I do miss the value.... It comes out like: 只有我会错过这个价值。

"WG","Werkgever","Cao"
"","",""

What do I do wrong? 我做错了什么?

Just use rows property to export data 只需使用行属性即可导出数据

$content = Get-Content -Raw $file | ConvertFrom-Json
$content.rows | export-csv 'somepath' -NoTypeInformation

尝试这个

get-content "C:\temp\test.txt" | ConvertFrom-Json | % {$_.Rows} | export-csv "C:\temp\test2.txt" -NoType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM