如何使用powershell从其他json文件创建json文件

eyh26e7m  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(148)

我想从一个json中获取值并传递给另一个json。我只需要几个参数,如name,severity,range,cwe,cvssand fixAvailable。这是我的json文件:

{
  "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
    }
}}

字符串
我想要这样的文件:

{
  "auditReportVersion": 2,
  "vulnerabilities": {
    "@babel/traverse": {
      "name": "@babel/traverse",
      "severity": "critical",
      "range": "\<7.23.2",
      "fixAvailable": true
}
}
}


需要帮助..谢谢

iyfjxgzm

iyfjxgzm1#

这里是你可以做到这一点的方法之一,使用ConvertFrom-JSON小工具将JSON输入转换为一个对象,然后创建一个具有所需属性设置的自定义对象,如果你想在JSON中使用它,你可以使用$obj | ConvertTo-JSON小工具。

$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]@{
        '@babel/traverse' = [pscustomobject]@{
            name = $inputObj.vulnerabilities.'@babel/traverse'.name
            severity = $inputObj.vulnerabilities.'@babel/traverse'.severity
            range = $inputObj.vulnerabilities.'@babel/traverse'.range
            fixAvailable = $inputObj.vulnerabilities.'@babel/traverse'.fixAvailable
        }
    }
}

$obj | ConvertTo-Json

字符串

相关问题