如何将嵌套的JSON转换为CSV的PowerShell脚本

huangapple go评论62阅读模式
英文:

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=&#39;C:\Users\&lt;redacted&gt;\&#39;
$InputFile= Read-Host -prompt &#39;Specify a target file&#39;
$OutputFile= Read-Host -prompt &#39;Specify a destination file&#39;
((Get-Content -Path &quot;$pathToJsonFile$InputFile&quot;) | ConvertFrom-Json -Depth 64).findings[0].packages | ConvertTo-Csv -NoTypeInformation | Set-Content &quot;$pathToJsonFile$OutputFile&quot;

More Info

Adjusting the original command:

((Get-Content -Path &quot;.\test.json&quot;) | ConvertFrom-Json -Depth 64).results | ConvertTo-Csv -NoTypeInformation | Set-Content .\test.csv

Where test.json contains:

{
    results: [
	    { name: &quot;Bill&quot;, age: 38 },
	    { name: &quot;Jeff&quot;, age: 39 },
	    { name: &quot;Ruby&quot;, age: 40 }	
    ]
}

Will yield a new file test.csv which contains:

&quot;name&quot;,&quot;age&quot;
&quot;Bill&quot;,&quot;38&quot;
&quot;Jeff&quot;,&quot;39&quot;
&quot;Ruby&quot;,&quot;40&quot;

huangapple
  • 本文由 发表于 2023年4月19日 23:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定