使用PowerShell将JSON转换为CSV [英] Convert JSON to CSV using PowerShell

查看:413
本文介绍了使用PowerShell将JSON转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例JSON格式的此处,如果我使用类似的格式,可以很好地转换: https://konklone.io/json/

I have a sample JSON-formatted here which converts fine if I use something like: https://konklone.io/json/

我已经在PowerShell中尝试了以下代码:

I've tried the following code in PowerShell:

(Get-Content -Path $pathToJsonFile | ConvertFrom-Json) 
| ConvertTo-Csv -NoTypeInformation 
| Set-Content $pathToOutputFile

但是我得到的唯一结果是:

But the only result I get is this:

{"totalCount":19,"resultCount":19,"hasMore":false,"results":

如何在PowerShell中正确转换?

How do I go about converting this correctly in PowerShell?

推荐答案

仅查看(Get-Content -Path $pathToJsonFile) | ConvertFrom-Json,看来JSON的其余部分都进入了results属性,因此我们可以得到我认为您想要的结果想要通过做:

By looking at just (Get-Content -Path $pathToJsonFile) | ConvertFrom-Json it looks like the rest of the JSON is going in to a results property so we can get the result I think you want by doing:

((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json).results |
    ConvertTo-Csv -NoTypeInformation |
    Set-Content $pathToOutputFile

仅供参考,您可以通过Export-CSV一键执行ConvertTo-CsvSet-Content:

FYI you can do ConvertTo-Csv and Set-Content in one move with Export-CSV:

((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json).results |
    Export-CSV $pathToOutputFile -NoTypeInformation

这篇关于使用PowerShell将JSON转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆