And*_*und 6 csv powershell json
我在这里有一个JSON格式的示例,如果我使用类似的东西,它会很好地转换:https://konklone.io/json/
我在PowerShell中尝试了以下代码:
(Get-Content -Path $pathToJsonFile | ConvertFrom-Json)
| ConvertTo-Csv -NoTypeInformation
| Set-Content $pathToOutputFile
Run Code Online (Sandbox Code Playgroud)
但我得到的唯一结果是:
{"totalCount":19,"resultCount":19,"hasMore":false,"results":
Run Code Online (Sandbox Code Playgroud)
如何在PowerShell中正确转换?
Mar*_*agg 11
通过查看(Get-Content -Path $pathToJsonFile) | ConvertFrom-Json它看起来像JSON的其余部分进入一个results属性,所以我们可以得到我认为你想要的结果:
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json).results |
ConvertTo-Csv -NoTypeInformation |
Set-Content $pathToOutputFile
Run Code Online (Sandbox Code Playgroud)
仅供参考,你可以做ConvertTo-Csv,并Set-Content与一招Export-CSV:
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json).results |
Export-CSV $pathToOutputFile -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)
小智 6
我从 REST Web api 获取 json,发现以下方法有效:
Invoke-WebRequest -method GET -uri $RemoteHost -Headers $headers
| ConvertFrom-Json
| Select-Object -ExpandProperty <Name of object in json>
| ConvertTo-Csv -NoTypeInformation
| Set-Content $pathToOutputFile
I end up with a perfectly formatted csv file
Run Code Online (Sandbox Code Playgroud)
您必须results使用Select-Objectcmdlet和-expand参数选择CSV中的属性:
Get-Content -Path $pathToJsonFile |
ConvertFrom-Json |
Select-Object -expand results |
ConvertTo-Csv -NoTypeInformation |
Set-Content $pathToOutputFile
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24242 次 |
| 最近记录: |