ndequeker
ndequeker

Reputation: 8000

Concatenate nested fields

I'm trying to concatenate two properties for a list of objects.

Input

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

Desired output

The top-level image property should be used as a prefix for the description of every object in the vulnerabilities list.

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

Current attempt

My current filter:

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

Output

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

Unfortunately, I only get the description field back with my current filter. I want the full vulnerability object with the modified description field.

Question

How can I concatenate the nested field and keep the other properties of the object too?

jqPlay

Upvotes: 0

Views: 69

Answers (1)

peak
peak

Reputation: 116967

The simplest solution would probably be:

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

In words: update all the .description values using .image as shown.

Equivalently, and perhaps less esoterically:

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

Upvotes: 2

Related Questions