【问题标题】:Concatenate nested fields连接嵌套字段
【发布时间】:2019-06-20 10:41:52
【问题描述】:

我正在尝试连接对象列表的两个属性。

输入

{
    "image": "golang:1.3",
    "unapproved": [
        "CVE-2016-5421",
        "CVE-2019-5010"
    ],
    "vulnerabilities": [
        {
            "featurename": "curl",
            "featureversion": "7.38.0-4+deb8u2",
            "vulnerability": "CVE-2016-5421",
            "namespace": "debian:8",
            "description": "Use-after-free vulnerability in libcurl before 7.50.1 allows attackers to control which connection is used or possibly have unspecified other impact via unknown vectors.",
            "link": "https://security-tracker.debian.org/tracker/CVE-2016-5421",
            "severity": "High",
            "fixedby": "7.38.0-4+deb8u4"
        },
        {
            "featurename": "python2.7",
            "featureversion": "2.7.9-2",
            "vulnerability": "CVE-2019-5010",
            "namespace": "debian:8",
            "description": "Test description",
            "link": "https://security-tracker.debian.org/tracker/CVE-2019-5010",
            "severity": "Unknown",
            "fixedby": ""
        }
    ]
}

期望的输出

顶级image 属性应该用作前缀 description vulnerabilities 列表中的每个对象。

{
  "image": "golang:1.3",
  "unapproved": [
    "CVE-2016-5421",
    "CVE-2019-5010"
  ],
  "vulnerabilities": [
    {
      "featurename": "curl",
      "featureversion": "7.38.0-4+deb8u2",
      "vulnerability": "CVE-2016-5421",
      "namespace": "debian:8",
      "description": "golang:1.3 - Use-after-free vulnerability in libcurl before 7.50.1 allows attackers to control which connection is used or possibly have unspecified other impact via unknown vectors.",
      "link": "https://security-tracker.debian.org/tracker/CVE-2016-5421",
      "severity": "High",
      "fixedby": "7.38.0-4+deb8u4"
    },
    {
      "featurename": "python2.7",
      "featureversion": "2.7.9-2",
      "vulnerability": "CVE-2019-5010",
      "namespace": "debian:8",
      "description": "golang:1.3 - Test description",
      "link": "https://security-tracker.debian.org/tracker/CVE-2019-5010",
      "severity": "Unknown",
      "fixedby": ""
    }
  ]
}

当前尝试

我当前的过滤器:

{image, unapproved, vulnerabilities: [{description: (.image + " - " + .vulnerabilities[].description)}] }

输出

{
  "image": "golang:1.3",
  "unapproved": [
    "CVE-2016-5421",
    "CVE-2019-5010"
  ],
  "vulnerabilities": [
    {
      "description": "golang:1.3 - Use-after-free vulnerability in libcurl before 7.50.1 allows attackers to control which connection is used or possibly have unspecified other impact via unknown vectors."
    },
    {
      "description": "golang:1.3 - Test description"
    }
  ]
}

不幸的是,我只能使用当前的过滤器返回 description 字段。我想要带有修改后的description 字段的完整漏洞对象。

问题

如何连接嵌套字段并保留对象的其他属性?

jqPlay

【问题讨论】:

    标签: json nested jq


    【解决方案1】:

    最简单的解决方案可能是:

    .image as $prefix
    | .vulnerabilities[].description |= $prefix + " - " + .
    

    简单来说:使用 .image 更新所有 .description 值,如图所示。

    同样地,也许不那么深奥:

    .image as $prefix
    | .vulnerabilities |= map(.description |= $prefix + " - " + .)
    

    【讨论】:

    • 我自己提出了以下解决方案:. as $top | . + { vulnerabilities: [.vulnerabilities[] | . + { description: ($top.image + " - " + .description) }] }。见jqplay.org/s/TWpQEejTEr 但你的更直观,非常感谢!
    猜你喜欢
    • 2014-07-05
    • 2015-06-11
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多