英文:
How to convert nested JSON to CSV powershell script
问题
I'm running into an issue where my Powershell code isn't working.
$pathToJsonFile='C:\Users<redacted>'
$InputFile= Read-Host -prompt 'Specify a target file'
$OutputFile= Read-Host -prompt 'Specify a destination file'
((Get-Content -Path $pathToJsonFile$InputFile) | ConvertFrom-Json -Depth 64).results | ConvertTo-Csv -NoTypeInformation | Set-Content $pathToJsonFile$OutputFile
The error I keep getting is:
> 4 | … om-Json -Depth 64).results | ConvertTo-Csv
> -NoTypeInformation | Set-C …
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> | Cannot bind argument to parameter 'InputObject' because it is null.
I'm not sure what the issue is since I'm piping one result into another. Referencing this post: https://stackoverflow.com/questions/43594860/convert-json-to-csv-using-powershell
I'm also not sure what Depth is used for because I have two-layers of nested JSON I'm trying to get to do the conversion correctly.
英文:
I'm running into an issue where my Powershell code isn't working.
$pathToJsonFile='C:\Users\<redacted>\'
$InputFile= Read-Host -prompt 'Specify a target file'
$OutputFile= Read-Host -prompt 'Specify a destination file'
((Get-Content -Path $pathToJsonFile$InputFile) | ConvertFrom-Json -Depth 64).results | ConvertTo-Csv -NoTypeInformation | Set-Content $pathToJsonFile$OutputFile
The error I keep getting is:
> 4 | … om-Json -Depth 64).results | ConvertTo-Csv
> -NoTypeInformation | Set-C …
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> | Cannot bind argument to parameter 'InputObject' because it is null.
I'm not sure what the issue is since I'm piping one result into another. Referencing this post: https://stackoverflow.com/questions/43594860/convert-json-to-csv-using-powershell
I'm also not sure what Depth is used for because I have two-layers of nested JSON I'm trying to get to do the conversion correctly.
答案1
得分: 1
以下是翻译好的部分:
问题在于原始命令中引用了一个在反序列化对象中不存在的results
属性,在您的模式中似乎不存在。如果目标是将不同的属性投影到CSV中,比如第一个findings
元素中的packages
数组,您可以将您的命令更改为如下(注意,我还在路径周围添加了引号):
$pathToJsonFile='C:\Users\<redacted>\'
$InputFile= Read-Host -prompt 'Specify a target file'
$OutputFile= Read-Host -prompt 'Specify a destination file'
((Get-Content -Path "$pathToJsonFile$InputFile") | ConvertFrom-Json -Depth 64).findings[0].packages | ConvertTo-Csv -NoTypeInformation | Set-Content "$pathToJsonFile$OutputFile"
更多信息
调整原始命令:
((Get-Content -Path ".\test.json") | ConvertFrom-Json -Depth 64).results | ConvertTo-Csv -NoTypeInformation | Set-Content .\test.csv
其中test.json
包含:
{
results: [
{ name: "Bill", age: 38 },
{ name: "Jeff", age: 39 },
{ name: "Ruby", age: 40 }
]
}
将生成一个名为test.csv
的新文件,其中包含:
"name","age"
"Bill","38"
"Jeff","39"
"Ruby","40"
英文:
The issue is that the original command addresses a results
property in the deserialized object, which doesn't appear to exist in your schema. If the objective is to project a different property, say the packages
array in the first findings
element, to a CSV, you'd want to change your command to something like (note I added the quotes around the paths too):
$pathToJsonFile='C:\Users\<redacted>\'
$InputFile= Read-Host -prompt 'Specify a target file'
$OutputFile= Read-Host -prompt 'Specify a destination file'
((Get-Content -Path "$pathToJsonFile$InputFile") | ConvertFrom-Json -Depth 64).findings[0].packages | ConvertTo-Csv -NoTypeInformation | Set-Content "$pathToJsonFile$OutputFile"
More Info
Adjusting the original command:
((Get-Content -Path ".\test.json") | ConvertFrom-Json -Depth 64).results | ConvertTo-Csv -NoTypeInformation | Set-Content .\test.csv
Where test.json
contains:
{
results: [
{ name: "Bill", age: 38 },
{ name: "Jeff", age: 39 },
{ name: "Ruby", age: 40 }
]
}
Will yield a new file test.csv
which contains:
"name","age"
"Bill","38"
"Jeff","39"
"Ruby","40"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论