how to get values of json in powershell using foreach loop

103 views Asked by At

I want to parse thought my json object using for loop,I have multiple objects like "@babel/traverse" and i would like to parse them to create json file with minimum fields as mentiond in the code, I am getting error at foreach loop.here is what i tried:

$jsonInput=@"
{
    "auditReportVersion": 2,
    "vulnerabilities": {
        "@babel/traverse": {
            "name": "@babel/traverse",
            "severity": "critical",
            "isDirect": false,
            "via": [
                {
                    "source": 1094446,
                    "name": "@babel/traverse",
                    "dependency": "@babel/traverse",
                    "title": "Babel vulnerable to arbitrary code execution when compiling specifically crafted malicious code",
                    "url": "https://github.com/advisories/GHSA-67hx-6x53-jw92",
                    "severity": "critical",
                    "cwe": [
                        "CWE-184",
                        "CWE-697"
                    ],
                    "cvss": {
                        "score": 9.3,
                        "vectorString": "CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H"
                    },
                    "range": "<7.23.2"
                }
            ],
            "effects": [],
            "range": "<7.23.2",
            "nodes": [
                "node_modules/@babel/traverse"
            ],
            "fixAvailable": true
        }
    }
}
"@

$inputObj = $jsonInput | ConvertFrom-Json



$obj = [pscustomobject]@{
    auditReportVersion = $inputObj.auditReportVersion
    vulnerabilities = [pscustomobject]@{
        foreach ($object in $jsonInput.PSObject.Properties) {
         $objectproperties = Get-Member -InputObject $object.Value -MemberType NoteProperty
        foreach($property in $objectproperties) {
        '$($property.Name)' = [pscustomobject]@{
            name = $inputObj.vulnerabilities.'$($property.Name)'.name
            severity = $inputObj.vulnerabilities.'$($property.Name)'.severity
            range = $inputObj.vulnerabilities.'$($property.Name)'.range
            fixAvailable = $inputObj.vulnerabilities.'$($property.Name)'.fixAvailable
        }
    }}
    }
}


$obj | ConvertTo-Json -Depth 4 | Out-File .\csv.json

I get error at foreachloop.I want to parse json using foreach loop..please help thanks

0

There are 0 answers