Home > database >  how to using jq command to Delete the specified multi-line field
how to using jq command to Delete the specified multi-line field

Time:02-14

I want to use the jq command to filter out the architecture I want in the manifest file of docker

cat manifest.json |jq -r
{
  "schemaVersion": 2,
  "mediaType": "application/vnd.docker.distribution.manifest.list.v2 json",
  "manifests": [
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2 json",
      "size": 1785,
      "digest": "sha256:34860ea294a018d392e61936f19a7862d5e92039d196cac9176da14b2bbd0fe3",
      "platform": {
        "architecture": "amd64",
        "os": "linux"
      }
    },
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2 json",
      "size": 1996,
      "digest": "sha256:93d5f2293f6faf76fc3f2ff30628cf2100b68e3cf16901f39f01d449e67f67d9",
      "platform": {
        "architecture": "arm",
        "os": "linux"
      }
    },
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2 json",
      "size": 1996,
      "digest": "sha256:a0000b627744766025f552c4db171a00b055d5d709f22aec8be96b6375b220cf",
      "platform": {
        "architecture": "arm64",
        "os": "linux"
      }
    },
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2 json",
      "size": 1995,
      "digest": "sha256:227c06dc0468dacfcfc04cc0aad3f5138a51bb2afa4d8252c6e83ca39beb8c6f",
      "platform": {
        "architecture": "ppc64le",
        "os": "linux"
      }
    },
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2 json",
      "size": 1996,
      "digest": "sha256:e7cfbfe13f7a179277f55a835abc0b01f9c6c21892c1ae1b0007f2e09777bca0",
      "platform": {
        "architecture": "s390x",
        "os": "linux"
      }
    }
  ]
}
cat manifest.json |jq -r 'del(.manifests[] |select(.platform.architecture=="arm" and .platform.architecture=="ppc64le"))'

The above command does not take effect. If I only want to keep the amd64 and arm64 architectures in the manifest file, how should I write this command?

CodePudding user response:

You should use or instead of and since a field can only have one of those options (if at all), never both.

According to "I only want to keep the amd64 and arm64 architectures", instead of deleting you can simply go the keeping route:

jq '
  .manifests |= map(select(
    .platform.architecture == "arm64" or .platform.architecture == "amd64"
  ))
'

Demo

If you're using jq 1.6 or later, you can simplify this using the IN filter:

jq '
  .manifests |= map(select(
    IN(.platform.architecture; "arm64", "amd64")
  ))
'

Demo

Output

{
  "schemaVersion": 2,
  "mediaType": "application/vnd.docker.distribution.manifest.list.v2 json",
  "manifests": [
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2 json",
      "size": 1785,
      "digest": "sha256:34860ea294a018d392e61936f19a7862d5e92039d196cac9176da14b2bbd0fe3",
      "platform": {
        "architecture": "amd64",
        "os": "linux"
      }
    },
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2 json",
      "size": 1996,
      "digest": "sha256:a0000b627744766025f552c4db171a00b055d5d709f22aec8be96b6375b220cf",
      "platform": {
        "architecture": "arm64",
        "os": "linux"
      }
    }
  ]
}
  • Related