连接嵌套字段

时间:2019-06-20 10:41:52

标签: json nested jq

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

输入

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

1 个答案:

答案 0 :(得分:2)

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

zonesViewModel.merchantInfoBottomLayout!!.state = BottomSheetBehavior.STATE_COLLAPSED

换句话说:如图所示,使用.image更新所有zonesViewModel.merchantInfoBottomLayout!!.state = BottomSheetBehavior.STATE_SETTLING值。

等效地,也许更不深奥地:

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