0

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: 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.

6
  • 1
    Sounds like the json document doesn't have a results top-level property. Commented Apr 19, 2023 at 15:21
  • So, Depth is just how many nested layers do you want to go into the JSON. To your main point, you won't be able to directly convert nested JSON into csv. You will have to do some data manipulation to do this. Commented Apr 19, 2023 at 15:22
  • 1
    Could you put an example of your JSON? Commented Apr 19, 2023 at 15:24
  • { "image_name": "", "docker_image_id": "", "tag": "", "platform": "docker", "findings": [ { "nvdFinding": { "published_date": "", "cpe": [], "remediation": "", "references": [ "sourceware.org/bugzilla/show_bug.cgi?id=28768", "lists.debian.org/debian-lts-announce/2022/10/msg00021.html" ] }, "packages": [ { "name": "libc-bin", "version": "2.28-10+deb10u1", "type": "linux" } ] }, ... } Commented Apr 19, 2023 at 15:43
  • It's a bit butchered Commented Apr 19, 2023 at 15:46

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.