bashjq

How to join a string onto the output from an array


Let's say this is my output from an aws-cli command.

{
    "imageIds": [
        {
            "imageDigest": "sha256:9"
        },
        {
            "imageDigest": "sha256:5"
        },
        {
            "imageDigest": "sha256:46",
            "imageTag": "latest"
        }
    ]
}

Using:

--query="imageIds[?imageTag != 'latest'].imageDigest"

Gives me ["sha256:9", "sha256:5", "sha256:46"].

How do I join an additional string to that list, either using the query, jq or bash, so it looks like ["X sha256:9", "X sha256:5", "X sha256:46"]?


Solution

  • $ echo '["sha256:9", "sha256:5", "sha256:46"]' | jq 'map("X " + .)'
    [
      "X sha256:9",
      "X sha256:5",
      "X sha256:46"
    ]
    

    Note that strings are delimited with double quotes, not single quotes.